I am coding the previous/next buttons of a music player. I need these UIButtons to have dual purpose.
1. Tap – Previous/Next song
2. Long Press – Seek
That works, but what got me was that I didn’t know when the end of the long press was so I never knew when to stop seeking. I tried to attach an action to the UIButton on the UIControlEventTouchCancel event, but that is not correct.
This is my setup code:
gestureSeekBackward = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(seekBackward:)]; gestureTapBackward = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(previousSong:)]; gestureTapBackward.numberOfTapsRequired = 1; [backwardButton addGestureRecognizer:gestureSeekBackward]; [backwardButton addGestureRecognizer:gestureTapBackward]; |
What I needed to do was check whether the state is ended for the seek gesture. I checked gesture.state to see whether it was equal to UIGestureRecognizerStateEnded.
-(void) seekBackward:(UILongPressGestureRecognizer *)gesture{ [musicPlayer beginSeekingBackward]; seeking = YES; //we know! if(gesture.state == UIGestureRecognizerStateEnded) { [self endSeek]; //If it's over let's wrap it up. } } |
This was the perfect solution. Not only do my buttons do double duty but seeking stops automatically when you lift your finger.