UIStepper Swift

import UIKit

class ViewController: UIViewController {

var label : UILabel!

var stepper : UIStepper!

override func viewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.

label = UILabel(frame:CGRectMake(200, 100, 40, 30))

label.backgroundColor = UIColor.cyanColor()

label.textAlignment = NSTextAlignment.Center;

label.textColor = UIColor.orangeColor()

label.text = "0"

label.layer.cornerRadius = 5.0;

label.clipsToBounds = true;

self.view .addSubview(label)

stepper = UIStepper(frame:CGRectMake(100, 100, 0, 0))

self.view.addSubview(stepper)

//设置stepper的范围与初始值

stepper.maximumValue=10

stepper.minimumValue=0

stepper.value=0

//设置每次增减的值

stepper.stepValue=1

//设置stepper可以按住不放来连续更改值

stepper.continuous=true

//设置stepper是否循环(到最大值时再增加数值从最小值开始)

stepper.wraps=false

stepper.addTarget(self, action: #selector(stepperAction), forControlEvents: .ValueChanged)

}

func stepperAction(){

label.text = "\(Int(stepper.value))"

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

}

你可能感兴趣的:(UIStepper Swift)