2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

UISliderをタップで選択可能にする (iOS12以上の場合)

Last updated at Posted at 2019-12-26

UISliderをタップで選択可能にするには

iOS12より前
class MySlider: UISlider {
    override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
        return true
    }
}

しかしiOS12以降だとSliderの値が変わらない

iOS12以降
class MySlider: UISlider {
    override func beginTracking(_ touch: UITouch, with event: UIEvent?) -> Bool {
        let tapPoint = touch.location(in: self)
        let fraction = Float(tapPoint.x / bounds.width)
        let newValue = (maximumValue - minimumValue) * fraction + minimumValue
        if newValue != value {
            value = newValue
        }
        return true
    }
}

これでiOS12以降でもタップで選択可能になり、.valueChangedイベントも送ってくれます

参考

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?