8.18 常用的一些控件,代码版

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

//        改下背景色,方便查看
        self.view.backgroundColor = UIColor.grayColor()

//        UIsegmentedControl
        let titles = ["One", "Two", "Three"]
        let segmentCtrl = UISegmentedControl(items: titles)
        segmentCtrl.frame = CGRect(x: 100, y: 100, width: 200,height: 50)
        segmentCtrl.momentary = false //改为true可以点击多次
        segmentCtrl.addTarget(self, action: #selector(didChanged(_:)), forControlEvents: .ValueChanged)
        self.view.addSubview(segmentCtrl)
        
//      UISlider
        let slider = UISlider(frame: CGRect(x: 100, y: 200, width: 200, height: 50))
        slider.addTarget(self, action: #selector(didSlider(_:)), forControlEvents: .ValueChanged)
        slider.minimumValue = 100
        slider.maximumValue = 200
        slider.value = 150
        slider.continuous = false
        self.view.addSubview(slider)
        
//        UISwitch
        let sw = UISwitch(frame: CGRect(x: 100, y: 300, width: 100, height: 50))
        sw.addTarget(self, action: #selector(didSwitch(_:)), forControlEvents: .ValueChanged)
        self.view.addSubview(sw)
        
//        UIPageControl
        let page = UIPageControl(frame: CGRect(x: 100, y: 350, width: 200, height:50))
        page.numberOfPages = 1
       page.pageIndicatorTintColor = UIColor.redColor()     //改变未选中白点颜色
       page.currentPageIndicatorTintColor = UIColor.yellowColor()     
       //改变小白点的颜色
       page.addTarget(self, action: #selector(didPage(_:)), forControlEvents: .ValueChanged)
        self.view.addSubview(page)
        
//        UIStepper
        let stepper = UIStepper(frame: CGRect(x: 150, y: 450, width: 200, height: 50))
        stepper.minimumValue = 0
        stepper.maximumValue = 20
        stepper.continuous = true

        page.addTarget(self, action: #selector(didStepper(_:)), forControlEvents: .ValueChanged)
        self.view.addSubview(stepper)
        
    }
    
    func didChanged(sender: UISegmentedControl) {
        print(sender.selectedSegmentIndex)
    }

    func didSlider(sender: UISlider) {
        print(sender.value)
    }

    func didSwitch(sender: UISwitch) {
        print(sender.on)
    }

    func didPage(sender: UIPageControl) {
        print(sender.numberOfPages)
    }
    func didStepper(sender: UIStepper) {
        print(sender.value)
    }
}

编译运行结果如下:

8.18 常用的一些控件,代码版_第1张图片
Paste_Image.png

你可能感兴趣的:(8.18 常用的一些控件,代码版)