class ViewController: UIViewController {
var stepper:UIStepper!
var label:UILabel!
override func viewDidLoad() {
super.viewDidLoad()
stepper = UIStepper(frame: CGRect(x: 60.0, y: 60.0, width: 100.0, height: 100.0))
stepper.maximumValue = 10
stepper.minimumValue = 1
stepper.stepValue = 0.5 // 每次增减的值
stepper.isContinuous = true // 连续按动
stepper.wraps = true // 到最大值时是否循环
stepper.addTarget(self, action: #selector(stepperValueChanged(stepper:)), for: .valueChanged)
self.view.addSubview(stepper)
label = UILabel(frame: CGRect(x: 60.0, y: 200.0, width: 100.0, height: 200.0))
label.font = UIFont.systemFont(ofSize: 10.0)
label.text = "当前值为: \(stepper.value)"
self.view.addSubview(label)
}
@objc func stepperValueChanged(stepper:UIStepper) ->Void {
label.text = "当前值为: \(stepper.value)"
}
}
网友评论