Swift - UIStepper

作者: iOS分享 | 来源:发表于2019-01-06 23:23 被阅读0次
    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)"
        }
    }
    

    相关文章

      网友评论

        本文标题:Swift - UIStepper

        本文链接:https://www.haomeiwen.com/subject/fxzerqtx.html