How to enable “tap and slide” in a UISlider?

后端 未结 14 544
不知归路
不知归路 2020-12-03 20:54

What I want to get is a UISlider which lets the user not only slide when he starts on its thumbRect, but also when he taps elsewhere. When the user

14条回答
  •  时光取名叫无心
    2020-12-03 21:33

    To expand on the answer of Khang Azun- for swift 5 put the following in a UISlider custom class:

    override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
        let percent = Float(touch.location(in: self).x / bounds.size.width)
        let delta = percent * (maximumValue - minimumValue)
    
        let newValue = minimumValue + delta
        self.setValue(newValue, animated: false)
        super.sendActions(for: UIControl.Event.valueChanged)
        return true
    }
    

提交回复
热议问题