Iphone – In UIKit, how to detect when a UISlider has stopped moving but has not been released

audiococoa-touchiphone

I have a sound effect that I want to only play when the slider is moving.

Currently I have the playSound method fire when the UISlider is sending Value Changed events.

I need a way to detect when the slider is not being moved, yet hasn't been released.

I think I can use the Control Events of it's super class to work out the timing but I don't see how to detect them.

Any ideas?

Best Answer

If I understand you correctly, you want to keep playing the sound as long as the slider is touched, whether it's moving or not. To do this, register one method as the target for UIControlEventTouchDown (and perhaps UIControlEventTouchDragInside) events and another for some combination of UIControlEventTouchDragExit/TouchUpOutside/TouchUpInside/TouchCancel (depending on what you need). The first method starts playing the sound and the second one stops it.

If you want to play another sound when the slider is still touched but not moving, I would recommend starting a timer each time you receive a TouchDown/ValueCahanged/etc event:

self.touchTimer = [NSTimer scheduledTimerWithTimeInterval: kDelay
                                                target:self
                                              selector:@selector(noMovement:)
                                              userInfo:nil
                                               repeats:NO];

Then, whenever you receive another ValueChanged even, you cancel the timer and trigger another (or, better, reschedule the original one). When the timer triggers, it means the user hasn't moved the slider since kDelay, and you can change the sound being played. (You need to cancel the timer when you get a TouchUpInside/Outside event too.)