Detecting touches on a UISlider?

后端 未结 3 1186
南笙
南笙 2020-12-18 22:40

I have a UISlider on screen, and I need to be able to detect when the user stops touching it. (so I can fade some elements away).

I have tried using:

-

3条回答
  •  执念已碎
    2020-12-18 23:04

    I couldn't get anything to capture both the start and end of the touches, but upon RTFD-ing, I came up with something that will do both.

      @IBAction func sliderAction(_ sender: UISlider, forEvent event: UIEvent) {
    
        if let touchEvent = event.allTouches?.first {
          switch touchEvent.phase {
          case .began:
            print("touches began")
            sliderTouchBegan()
          case .ended:
            print("touches ended")
            sliderTouchEnded()
          default:
            delegate?.sliderValueUpdated(sender.value)
          }
        }
      }
    

    sliderTouchBegan() and sliderTouchEnded() are just methods I wrote that handle animations when the touch begins and when it ends. If it's not a begin or end, it's a default and the slider value updates.

提交回复
热议问题