In UIKit, how to detect when a UISlider stopped moving but wasn’t released

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

I currently have a playSound method fire when a UISlider dispatches events with a modified value.

I need a way to detect when the slider is not moving but has not been released.

I think I can use the control events of this superclass to determine the time, but I don’t see how to detect them.

Any ideas?

+4
source share
2 answers

If I understand you correctly, you want to continue to play the sound until the slider touches whether it moves or not. To do this, register one method as an object for UIControlEventTouchDown events (and, possibly, UIControlEventTouchDragInside), and the other for some combination of UIControlEventTouchDragExit / TouchUpOutside / TouchUpInside / TouchCancel (depending on what you need). The first method starts to play the sound, and the second stops it.

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

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

Then, whenever you get another ValueChanged, you cancel the timer and start another (or, better, move the original one). When the timer starts, it means that the user has not moved the slider with kDelay, and you can change the playback sound. (You need to cancel the timer when you receive the TouchUpInside / Outside event.)

+3
source

I am doing something similar and I just created my own subclass of UISlider. I implemented the standard functions of the UIResponder touch interface and executed my own code before calling the superclass. Using this approach, you will get fine-grained access to information about the sensor phase, and you can put a timer in the slider object (seems to be the best OO way to do this?)

Here is some code to let you add your own behavior to TouchBegan. You can also override touchphonesMoved and touchesEnded depending on your needs. As you move on to the superclass, there should be no change in the behavior of the slider.

In my case, I want the gray UISlider track to be clickable, so I manually set the value of the slider based on the location of the touch.

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // set our value, if necessary CGRect t = [self trackRectForBounds: [self bounds]]; float v = [self minimumValue] + ([[touches anyObject] locationInView: self].x - t.origin.x - 4.0) * (([self maximumValue]-[self minimumValue]) / (t.size.width - 8.0)); [self setValue: v]; [super touchesBegan: touches withEvent: event]; 

}

Hope this helps,

+1
source

Source: https://habr.com/ru/post/1285786/


All Articles