iPhone : How to detect the end of slider drag?

后端 未结 16 2555
不思量自难忘°
不思量自难忘° 2020-12-02 07:36

How to detect the event when the user has ended the drag of a slider pointer?

相关标签:
16条回答
  • 2020-12-02 08:12

    Swift 5 and Swift 4

    1. Add target for touchUpInside and touchUpOutside events. You can do it either programatically or from storyboard. This is how you do it programatically

      slider.addTarget(self, action: #selector(sliderDidEndSliding), for: [.touchUpInside, .touchUpOutside])
      
    2. Write your function to handle the end of slider drag

      func sliderDidEndSliding() {
          print("end sliding")
      }
      
    0 讨论(0)
  • 2020-12-02 08:14

    I had similar issue recently. Here is what I did in Swift:

        theSlider.continuous = false;
    

    The code that holds this continuous value reads:

    public var continuous: Bool // if set, value change events are generated
        // any time the value changes due to dragging. default = YES
    

    The default seems to be YES (true). Setting it it to false does the thing you want.

    0 讨论(0)
  • 2020-12-02 08:16

    Maybe you should add target for UIControlEventTouchUpInside、UIControlEventTouchUpOutside、UIControlEventTouchCancel events.

    I met the same problem before , because i don't add target for UIControlEventTouchCancel event.

    0 讨论(0)
  • 2020-12-02 08:19

    Since UISlider is a subclass of UIControl, you can set a target and action for its UIControlEventTouchUpInside.

    If you want to do it in code, it looks like this:

    [self.slider addTarget:self action:@selector(dragEndedForSlider:)
        forControlEvents:UIControlEventTouchUpInside];
    

    That will send you a dragEndedForSlider: message when the touch ends.

    If you want to do it in your nib, you can control-click your slider to get its connections menu, and then drag from the “Touch Up Inside” socket to the target object.

    You should also add a target and action for UIControlEventTouchUpOutside and for UIControlEventTouchCancel.

    0 讨论(0)
  • 2020-12-02 08:20

    Create an action and outlet for the slider (or you can typecast sender from action to UISlider), and in the UI Uncheck the Continuous Updates check box. This way we will get the slider value only when slider stops dragging.

    @IBAction func actionSlider(_ sender: Any) {
       let value = sliderSpeed.value.rounded()
    }
    

    0 讨论(0)
  • 2020-12-02 08:23

    RxSwift:

    self.slider.rx.controlEvent([.touchUpInside, .touchUpOutside])
        .bind(to: self.viewModel.endSlidingSlider)
        .disposed(by: self.disposeBag)
    
    0 讨论(0)
提交回复
热议问题