UISwitch UISlider UISegmentControl UIStepper控件

import UIKit

class ViewController: UIViewController {

var lable:UILabel!

override func viewDidLoad() {

super.viewDidLoad()

self.view.backgroundColor = #colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: 1)

//UIController 控制控件的基类,凡是继承这个类的子类都可以实现addTarget(target:,actin:,controlEvents:)

//UIControllerd的子类 :UISwitch(开关控制控件)  UISlider(滑块控件)  UISegmentControl(分段控制器) UIStepper(计步控件  自学)

let aSwitch = UISwitch()

aSwitch.frame.origin = CGPoint(x: 50, y: 50)

//打开时的内部渲染颜色

aSwitch.onTintColor = #colorLiteral(red: 0.9372549057, green: 0.3490196168, blue: 0.1921568662, alpha: 1)

//控制边框颜色

aSwitch.tintColor = #colorLiteral(red: 0.2196078449, green: 0.007843137719, blue: 0.8549019694, alpha: 1)

//滑块的颜色

aSwitch.thumbTintColor = #colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1)

//ios7之后弃用

//aSwitch.onImage = UIImage(named:"1.png")

//aswitch的关联方法

aSwitch.addTarget(self, action: #selector(switchAction), for: .valueChanged)

self.view.addSubview(aSwitch)

//UISilder控件 滑块控件(调节声音,屏幕亮度,播放进度,字体大小)

let aSlider = UISlider(frame: CGRect(x: 30, y: 140, width: 354, height: 30))

self.view.addSubview(aSlider)

//滑块当前位置代表的数值

aSlider.value = 0.5

//设置滑块的最小值

aSlider.minimumValue = 0.0

//设置滑块的最大值

aSlider.maximumValue = 10.0

//设置划过区域的颜色

aSlider.minimumTrackTintColor = #colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1)

//设置未划过区域的颜色

aSlider.maximumTrackTintColor = #colorLiteral(red: 0.4666666687, green: 0.7647058964, blue: 0.2666666806, alpha: 1)

//设置滑块的颜色

aSlider.thumbTintColor = #colorLiteral(red: 0.9372549057, green: 0.3490196168, blue: 0.1921568662, alpha: 1)

//设置滑块的图片

aSlider.setThumbImage(#imageLiteral(resourceName: "a.png"), for:.normal)

aSlider.setThumbImage(#imageLiteral(resourceName: "a.png"), for:.highlighted)

//给滑块关联事件

aSlider.addTarget(self, action: #selector(sliderAction), for: .valueChanged)

self.view.addSubview(aSlider)

//UISegmentedControl  分段控制器,你可以把它理解为一个存放多个button的控件

let titles = ["红色","绿色背景","蓝色"]

let segmentedControl = UISegmentedControl(items: titles)

segmentedControl.frame = CGRect(x: 132, y: 30, width: 150, height: 40)

//设置分段的宽度  下标为1

segmentedControl.setWidth(80, forSegmentAt: 1)

//设置默认选中的分段  数组中下标为0

segmentedControl.selectedSegmentIndex = 0

//设置边框,选中分段的颜色

segmentedControl.tintColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)

self.view.addSubview(segmentedControl)

//给segmentedControl添加关联事件

segmentedControl.addTarget(self, action:#selector(segmentedAction), for: .valueChanged)

//UIStepper(计步控件)  UIPageControl

let ste = UIStepper()

ste.frame.origin = CGPoint(x: 100, y: 300)

ste.backgroundColor = #colorLiteral(red: 0.8549019694, green: 0.250980407, blue: 0.4784313738, alpha: 1)

ste.minimumValue = 0

ste.maximumValue = 10

ste.autorepeat = true

//设置是否循环和是否连续

ste.isContinuous = true

//判断是否循环,即到最大值后又从最小值开始

ste.wraps = true

ste.tintColor = #colorLiteral(red: 0.1764705926, green: 0.01176470611, blue: 0.5607843399, alpha: 1)

//设置背景图片

ste.setBackgroundImage(UIImage(named:"a.png"), for:.normal)

ste.addTarget(self, action: #selector(steAction), for: .allTouchEvents)

self.view.addSubview(ste)

lable = UILabel(frame: CGRect(x: 50, y: 300, width: 30, height: 30))

lable.backgroundColor = #colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1)

self.view.addSubview(lable)

}

//MARK: - steAction关联方法

func steAction(sender:UIStepper) {

print(sender.value)

self.lable.text = String(sender.value)

}

//MARK: - segmentedControl关联方法

func segmentedAction(sender:UISegmentedControl) {

//取出选中分段的下标值

switch sender.selectedSegmentIndex {

case 0:

self.view.backgroundColor = UIColor.red

case 1:

self.view.backgroundColor = UIColor.green

case 2:

self.view.backgroundColor = UIColor.blue

default:

print("出错了")

}

}

//MARK: - slider的关联方法

func sliderAction(sender:UISlider) {

//print(sender.value)

self.view.alpha = CGFloat(sender.value)

}

//MARK: - switch的关联方法

func switchAction(sender:UISwitch){

print(sender.isOn)

if sender.isOn {

print("打开移动蜂窝")

}else{

print("关闭移动蜂窝")

}

}

override func didReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

}

你可能感兴趣的:(UISwitch UISlider UISegmentControl UIStepper控件)