UIStepper的使用
import UIKit
class ViewController: UIViewController {
var label: UILabel?
override func viewDidLoad() {
super.viewDidLoad()
self.setupStepper()
}
func setupStepper() {
let stepper = UIStepper()
stepper.center = self.view.center
// 设置stepper的范围与初始值
stepper.maximumValue = 100
stepper.minimumValue = 1
stepper.value = 90
// 设置每次增减的值
stepper.stepValue = 10.0
// 设置stepper可以按住不放来连续更改值
stepper.isContinuous = true
// 设置stepper是否循环(到最大值时再添加数值从最小值开始)
stepper.wraps = true
// 设置点击事件
stepper.addTarget(self, action: #selector(stepperChange(_:)), for: .valueChanged)
// 加减符号图标,背景图片,中间分割线图片都可以替换成自己图片
let subImage = UIImage(named: "sub.png")
let addImage = UIImage(named: "add.png")
stepper.setDecrementImage(subImage, for: .normal)
stepper.setIncrementImage(addImage, for: .normal)
self.label = UILabel(frame: CGRect(x: 100, y: 100, width: 200, height: 40))
self.label?.text = "当前值为:\(stepper.value)"
self.view.addSubview(self.label!)
self.view.addSubview(stepper)
}
func stepperChange(_ stepper: UIStepper) {
self.label?.text = "当前值为:\(stepper.value)"
}
}