1,滑块的创建
1
2
3
4
5
6
|
var slider= UISlider (frame: CGRectMake (0,0,300,50)) slider.center= self .view.center slider.minimumValue=0 //最小值 slider.maximumValue=1 //最大值 slider.value=0.5 //当前默认值 self .view.addSubview(slider) |
2,设置滑块的值,同时有动画
1
|
slider.setValue(0.8,animated: true ) |
3,滑块值改变响应
1
2
3
4
5
6
|
slider.continuous= false //滑块滑动停止后才触发ValueChanged事件 slider.addTarget( self ,action: "sliderDidchange:" , forControlEvents: UIControlEvents . ValueChanged ) func sliderDidchange(slider: UISlider ){ println (slider.value) } |
4,滑块左右两边槽的颜色
1
2
|
slider.minimumTrackTintColor= UIColor .redColor() //左边槽的颜色 slider.maximumTrackTintColor= UIColor .greenColor() //右边槽的颜色 |
5,滑块后面槽线两侧添加图标
1
2
|
slider.minimumValueImage= UIImage (named: "voice+" ) //左边图标 slider.maximumValueImage= UIImage (named: "voice-" ) //右边图标 |
6,自定义滑块组件图片
1
2
3
4
5
6
|
//设置滑块右边部分的图片 slider.setMaximumTrackImage( UIImage (named: "slider_max" ),forState: UIControlState . Normal ) //设置滑块左边部分的图片 slider.setMinimumTrackImage( UIImage (named: "slider_min" ),forState: UIControlState . Normal ) //设置滑块的图片 slider.setThumbImage( UIImage (named: "slider_thumb" ),forState: UIControlState . Normal ) |
1
2
3
4
|
//设置滑块右边部分的图片-使用三宫格缩放(左右14像素不变,中间缩放) let imgTrackRight = UIImage (named: "slider_max" ) let imgRight = imgTrackRight!.stretchableImageWithLeftCapWidth(14, topCapHeight:0) slider.setMaximumTrackImage(imgRight, forState: UIControlState . Normal ) |