SegmentedControl、Slider、Switch

SegmentedControl

//分段控件: 提供单选的能力
 {
       let titles = ["1", "2", "3"]
        let segmented = UISegmentedControl(items: titles)
        segmented.frame = CGRectMake(100, 100, 200, 50)
        segmented.momentary = true //不记录状态
        segmented.addTarget(self, action: #selector(didSeg(_:)),         forControlEvents: .ValueChanged)
        self.view.addSubview(segmented)
}

func didSeg(sender: UISegmentedControl){
        print(sender.selectedSegmentIndex)
    }

Slider

{
//滑动条
        let slider = UISlider(frame: CGRect(x: 100, y: 200, width: 200, height: 100))
        slider.addTarget(self, action: #selector(didSlider(_:)), forControlEvents: .ValueChanged)
        slider.minimumValue = 100
        slider.maximumValue = 200
        slider.value = 150
        slider.continuous = false   //连续触发
        self.view.addSubview(slider)
}

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

Switch

{
//Switch
        let sw = UISwitch(frame: CGRect(x: 100, y: 300, width: 200, height: 100))
        sw.addTarget(self, action: #selector(didSwitch(_:)), forControlEvents: .ValueChanged)
        self.view.addSubview(sw)
        
        let page = UIPageControl(frame: CGRect(x: 100, y: 400, width: 200, height: 100))
        page.addTarget(self, action: nil, forControlEvents: .ValueChanged)
        page.numberOfPages = 4
        page.currentPage = 2
        self.view.addSubview(page)
}

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

你可能感兴趣的:(SegmentedControl、Slider、Switch)