iOS-Swift 开发的各种坑

Swift - UIStepper

2019-01-06  本文已影响0人  iOS分享
import UIKit

class ViewController: UIViewController{
    var stepper:UIStepper!
    var label:UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        //创建Stepper
        stepper = UIStepper(frame: CGRect(x: 150, y: 250, width: 100, height: 100))
        //设置最大值
        stepper.maximumValue = 10
        //设置最小值
        stepper.minimumValue = 0
        //设置初始值
        stepper.value = 1
        //设置递增减值
        stepper.stepValue = 0.5
        //设置是否可以按住连续更改值
        stepper.isContinuous = true
        //设置stepper是否循环(超过最大值从最小值开始)
        stepper.wraps = true
        //设置stepper颜色
        stepper.tintColor = UIColor.red
        //设置+-符号,背景,中间分隔线图片
        stepper.setDecrementImage(UIImage(named: "DecrementImage"), for: .normal)//设置-符号图片
        stepper.setIncrementImage(UIImage(named: "IncrementImage"), for: .normal)//设置+符号图片
        stepper.setBackgroundImage(UIImage(named:"BackImage"), for: .normal)
        stepper.setDividerImage(UIImage(named: "DividerImage"), forLeftSegmentState: .normal, rightSegmentState: .normal)//设置中间分割线图片
        //添加响应事件
        stepper.addTarget(self, action: #selector(ViewController.stepperValueChanged), for: .valueChanged)
        self.view.addSubview(stepper)
        label = UILabel(frame: CGRect(x: 150, y: 300, width: 300, height: 50))
        label.text = "当前值为:\(stepper.value)"
        self.view.addSubview(label)
    }
    @objc func stepperValueChanged()
    {
        label.text = "当前值为:\(stepper.value)"
    }
}
上一篇下一篇

猜你喜欢

热点阅读