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

后端 未结 14 566
不知归路
不知归路 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:24

    This works for me in iOS 13.6 & 14.0
    No need to add gesture only override beginTracking function like this :

    @objc
    private func sliderTapped(touch: UITouch) {
        let point = touch.location(in: self)
        let percentage = Float(point.x / self.bounds.width)
        let delta = percentage * (self.maximumValue - self.minimumValue)
        let newValue = self.minimumValue + delta
        if newValue != self.value {
            value = newValue
            sendActions(for: .valueChanged)
        }
    }
    
    override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
        sliderTapped(touch: touch)
        return true
    }
    

提交回复
热议问题