Check if user finished sliding on a continuous UISlider?

前端 未结 4 1534
渐次进展
渐次进展 2021-01-31 07:37

In my app, I have a couple of UISlider instances to change various values. The values are displayed right next to the slider, as well as rendered in a 3d space in another visibl

相关标签:
4条回答
  • 2021-01-31 08:11

    You can do this with simple target/actions.

    Set a target and action for the UIControlEventValueChanged event, and then another target and action for the UIControlEventTouchUpInside event. With the continuous property set to YES, the value changed event will fire as the slider changes value, while the touch up inside event will only fire when the user releases the control.

    0 讨论(0)
  • 2021-01-31 08:23

    In Swift 3:

    @IBAction func sliderValueChanged(_ slider: UISlider, _ event: UIEvent) {
        guard let touch = event.allTouches?.first, touch.phase != .ended else {
            // ended
            return
        }
        // not ended yet
    }
    
    0 讨论(0)
  • 2021-01-31 08:26

    I just had to do this, so I looked up touch properties, and used the full IBAction header.

    This should be a viable alternative for people who want some extra control, though Jas's is definitely easier on the code side.

    - (IBAction)itemSlider:(UISlider *)itemSlider withEvent:(UIEvent*)e;
    {
        UITouch * touch = [e.allTouches anyObject];
    
        if( touch.phase != UITouchPhaseMoved && touch.phase != UITouchPhaseBegan)
        {
           //The user hasn't ended using the slider yet.
        }
    
    }
    

    :D

    0 讨论(0)
  • 2021-01-31 08:36

    Also note you should connect the UIControlEventTouchUpOutside event as well in case the user drags his finger out of the control before lifting it.

    0 讨论(0)
提交回复
热议问题