0

i m new with iOS, and i want to know how to make my slider to react only when the the touch is ended. i figure that out with the method:

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

But i can't make it work.

Does anyone can help me with simple & clear example?

Please, answer programmatically, i don't use storyBoard.

Thanks in advance!!!

mor mor
  • 17
  • 1
  • 2
  • 7

4 Answers4

1

You can add gesture recognizer:

 UITapGestureRecognizer *tapGestureRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSliderTap:)] autorelease];
 [slider addGestureRecognizer:tapGestureRecognizer];

- (void)onSliderTap:(UIGestureRecognizer *)gestureRecognizer { 
   // handle event
}

or add some action on slider:

[slider addTarget:self action:@selector(onTouchEnded:) forControlEvents:UIControlEventTouchUpInside];
B.S.
  • 21,660
  • 14
  • 87
  • 109
0

You could hook up UIControlEventTouchUpInside and -Outside instead of -ValueChanged (if you're doing that right now).

Edit: or, al little more elegant I would say, use UIControlEventValueChanged, but also have the event provided as a parameter to your method:

[self.slider addTarget:self action:@selector(handleValueChanged:withEvent:) forControlEvents:UIControlEventValueChanged];

And then check the phase of the touch, to see if it's in the ended state.

fguchelaar
  • 4,779
  • 23
  • 36
0

Found the answer!

these guys helped me alot: Detecting touches on a UISlider?

with a small accuracy:

the action for the relevant slider assign to him as:

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

and the method signature is:

- (IBAction)touchEnded:(id) sender

with storyboard it seems to me a bit different to assign the action to the slider.

Thanks for the answers!

Community
  • 1
  • 1
mor mor
  • 17
  • 1
  • 2
  • 7
0

You have to set the property continuous.

Contains a Boolean value indicating whether changes in the sliders value generate continuous update events.

mySlider.continuous= NO;
Holger
  • 581
  • 5
  • 10