15

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:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

but this did not work when ending touches on a slider.

pb2q
  • 58,613
  • 19
  • 146
  • 147
Andrew Martin
  • 161
  • 1
  • 1
  • 3
  • 4
    Why not use a target+action with `UIControlEventTouchUpInside` or `UIControlEventTouchUpOutside`? –  Jun 10 '12 at 18:21
  • 1
    This is a great suggestion! I wasn't aware you could use `UIControlEventTouchUpInside` on a UISlider, but it works! :) – Andrew Martin Jun 10 '12 at 18:41

3 Answers3

21

You can detect when a touch ends using two control events; try

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

or

[slider addTarget:self action:@selector(touchEnded:) 
                       forControlEvents:UIControlEventTouchUpOutside];

If you want to detect both types of the touchesEnd event, use

[slider addTarget:self action:@selector(touchEnded:) 
   forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
6

Instead of using touchesEnded: (which shouldn't be used for this purpose anyway), attach an action to the UISlider's UIControlEventValueChanged event and set the continuous property of the UISlider to NO, so the event will fire when the user finishes selecting a value.

mySlider.continuous = NO;
[mySlider addTarget:self
          action:@selector(myMethodThatFadesObjects) 
          forControlEvents:UIControlEventValueChanged];
Evan Mulawski
  • 54,662
  • 15
  • 117
  • 144
  • 2
    It's a nice idea that works, but I was already using `UIControlEventValueChanged` to get live updates on where the UISlider was, so I couldn't do that if I set `continuous` to no. – Andrew Martin Jun 10 '12 at 18:42
  • So you want to get the value in realtime, but only apply that value when the user stops touching it? That should have been stated in your question. – Evan Mulawski Jun 10 '12 at 18:57
6

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.

Adrian
  • 16,233
  • 18
  • 112
  • 180
  • Which control event type are u using for the sliderAction() ? Is it UIControlEvents.allTouchEvents? – hamedazhar Jul 16 '18 at 12:06
  • @Hamed When you drag out the connection from the storyboard, make it an action and under arguments, make sure you specify sender AND (emphasis mine) event. – Adrian Jul 16 '18 at 13:50