47

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 visible part of the app.

The 3d part includes some rather heavy calculations, and right now it doesn't seem possible to update it live as the slider changes. That would imply that I'd have to set the slider's continuous property to NO, therefore only getting updates when the slider has finished changing.

I'd prefer to have the displayed value update live, however. Is there a way to have a slider that is continuous (so I can update my value-label in real time) and still sends some kind of message once the user has finished interacting with it? My gut feeling right now is to subclass UISlider and override the touchesEnded: method. Is that feasible?

4 Answers 4

75

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.

9
  • 1
    doesnt work anymore on iOS7, all of a sudden. 6 is still functional.
    – dklt
    Sep 27, 2013 at 8:10
  • @Jas, dont have sufficient test cases for a bugreport. I think its default iOS7 behaviour. As a quick dirty fix, I manually firing UIControlEventValueChanged/Up/Down inside a UISlider subclass.
    – dklt
    Sep 28, 2013 at 16:44
  • 8
    Was running into the same issue where this stopped working in iOS7. Fixed it by adding UIControlEventTouchCancel
    – manggit
    Oct 25, 2013 at 22:23
  • 3
    If the user touches the slide and moves his finger away from it, touchUpInside is never called. The Slider, however, only takes into consideration the X position of the finger, even if Y is now very far. In this case, UIControlEventTouchCancel isn't called either. Is there a more elegant way to handle this other than just creating a BOOL that becomes TRUE during a UIControlEventTouchCancel and becomes FALSE when a touchupoutside or touchupinside happens?
    – HSNN
    Sep 5, 2014 at 19:00
  • 1
    @MichaelPirotte that depends on the situation.. You probably don't want your users to guestimate where the 35% mark is and only update the display once they made their guess...
    – Mark
    Aug 10, 2017 at 13:46
17

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

5
  • tested on iOS 5 and on iOS 7, works well in both cases. I can't believe I didn't know there's also an event parameter being sent.
    – alex-i
    Oct 14, 2013 at 11:54
  • 3
    @user2545330 [slider addTarget:@selector(itemSlider:withEvent:) forControlEvents:UIControlEventValueChanged]; - this will call the method above when slider value changes. More info: developer.apple.com/library/ios/documentation/UIKit/Reference/…:
    – alex-i
    Nov 19, 2013 at 14:57
  • 2
    Note that if you do this in the Xib, you don't need to add any code besides what's in the answer. These comments make me think some coders might not read the manuals.
    – Stephen J
    Nov 21, 2013 at 19:37
  • [slider addTarget:self action:@selector(itemSlider:withEvent:) forControlEvents:UIControlEventValueChanged];
    – jose920405
    Jun 24, 2015 at 20:36
  • This question keeps getting up voted, so I want to be extra clear you only need alexi and jose's code if you don't, super-easily, ctrl-drag from the slider to the file's owner in the xib. Unless it is an isolated custom view, that code isn't necessary. If you don't know how to work with Xibs, you are missing out on much unneeded code.
    – Stephen J
    Feb 19, 2016 at 18:33
16

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

7

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
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.