Swift UISwitch/UIProgressView/UISlider

前端之家收集整理的这篇文章主要介绍了Swift UISwitch/UIProgressView/UISlider前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.UISwitch

开关视图,可以让用户快速的开关一个功能,比如蓝牙,wif等.

系统默认样式:

上面绿色的开启状态,下面的是关闭状态.

UISwitch的构成部分:


  1. //MARK: initSwitchView
  2. var coun : NSInteger = 0
  3. func initSwitchView() {
  4. let testSwitch = UISwitch.init(frame: CGRectMake(100,100,51,31))
  5. self.view.addSubview(testSwitch)
  6. // testSwitch.on = true // 设置开关状态
  7. // testSwitch.onTintColor = UIColor.redColor()
  8. testSwitch.tintColor = UIColor.blueColor()
  9. testSwitch.thumbTintColor = UIColor.redColor()
  10. testSwitch.setOn(true,animated: true) // 设置动画
  11. testSwitch.addTarget(self,action: #selector(ViewController.UISwitchValueChange),forControlEvents:UIControlEvents.ValueChanged)
  12. }
  13. //MARK: UISwitchValueChange 事件
  14. func UISwitchValueChange() {
  15. self.coun = self.coun + 1
  16. print("\(self.coun)")
  17. }
UISwitch的大小是W51H31,这固定在,在设置它的frame的时候可以不设置其大小.为什么是固定的呢?我们可以在IB里面看看就知道了.

拖一个控件在IB中,我们看看


这个控件比较简单,一般使用系统样式就可以了,主要是增加值改变事件.

2.UIProgressView

UIProgressView进度条样式.默认最小值是0,最大值是0且不可以改变.

UIProgressView的构成部分:


UIProgressView的样式:


UIProgressView的高度也是固定的,固定值是2.

  1. //MARK: initProcessView
  2. func initProcessView() {
  3. let testProcessView = UIProgressView.init(frame: CGRectMake(60,150,200,2))
  4. self.view.addSubview(testProcessView)
  5. testProcessView.tag = 1;
  6. // testProcessView.progress = 0.8
  7. // testProcessView.progressImage = UIImage.init(named: "1")
  8. // testProcessView.progressTintColor = UIColor.redColor()
  9. // testProcessView.trackTintColor = UIColor.greenColor()
  10. // testProcessView.trackImage = UIImage.init(named: "2")
  11. // testProcessView.progressViewStyle = .Bar //样式
  12. NSTimer.scheduledTimerWithTimeInterval(1,target: self,selector: #selector(ViewController.UIProgressViewvalueChange),userInfo: nil,repeats: true)
  13. }
  14. //MARK: UIProgressViewvalueChange 事件
  15. func UIProgressViewvalueChange() {
  16. let testProcessView = self.view.viewWithTag(1) as! UIProgressView
  17. if testProcessView.progress == 1 {
  18. testProcessView.progress = 0
  19. }
  20. testProcessView.progress = testProcessView.progress + 0.05
  21. }
3.UiSlider

UiSlider感觉就是在UIProgressView的上面增加了一个用户可以手动控制的按钮,且最大值和最小值都可以由用户自行设置.

UiSlider的构成:

UiSlider的高度默认是31,我们看看UISwitch就知道了,它们有点联系了.

  1. //MARK: initSliderView
  2. func initSliderView() {
  3. let testSlider = UiSlider.init(frame: CGRectMake(60,31))
  4. self.view.addSubview(testSlider)
  5. testSlider.minimumValue = 0 //设置最小值
  6. testSlider.maximumValue = 100 //设置最大值
  7. // testSlider.value = 10 //设置当前值
  8. testSlider.setValue(20,animated: true) // 设置当前值 是否加动画效果
  9. testSlider.continuous = true
  10. testSlider.minimumValueImage = UIImage.init(named: "1")
  11. testSlider.maximumValueImage = UIImage.init(named: "2")
  12. testSlider.thumbTintColor = UIColor.redColor()
  13. testSlider.addTarget(self,action: #selector(ViewController.UiSliderValueChage),forControlEvents: UIControlEvents.ValueChanged)
  14. }
  15. //MARK: UiSliderValueChage 事件
  16. func UiSliderValueChage(testSlider : UiSlider) {
  17. print("\(testSlider.value)")
  18. }
UiSlider的continuous属性默认是true,这样当它的值改变的时候,值改变事件就可以关联起来,设置为false时,值改变事件就无效.
三个控件的效果图:

猜你在找的Swift相关文章