Swift—UISlider的常见用法

//创建

let slider = UISlider()
slider.frame = CGRect(x: 50, y: 100, width: 200, height: 50)

//最小/大值

slider.minimumValue = 0
slider.maximumValue = 1

//当前值(在取值范围之内的位置)

slider.value = 0.5

//按钮最小/大端图片

slider.minimumValueImage = UIImage(named:"SSS")
slider.maximumValueImage = UIImage(named:"DDD")

//不同状态之下,圆点显示图片

slider.setThumbImage(UIImage(named:""), for: .normal)

slider.setThumbImage(UIImage(named:""), for: .highlighted)

slider.setThumbImage(UIImage(named:""), for: .disabled)

slider.setThumbImage(UIImage(named:""), for: .selected)

//设置圆点的颜色

slider.thumbTintColor = UIColor.red

//设置划过和没有划过颜色(圆点左/右)

slider.minimumTrackTintColor = UIColor.yellow
slider.maximumTrackTintColor = UIColor.brown

//改变事件

// 滑块滑动停止后才触发ValueChanged事件 
slider.isContinuous = false

slider.addTarget(self, action: #selector(ViewController.change(slider:)), for: UIControlEvents.valueChanged)

slider.addTarget(self, action: #selector(ViewController.sliderDragUp(sender:)), for: UIControlEvents.touchUpInside)

func change(slider:UISlider) {
       print("slider.value = %d",slider.value)
}

func sliderDragUp(sender: UISlider) {
    print("value:(sender.value)")
}

你可能感兴趣的:(Swift—UISlider的常见用法)