Reputation: 22966
I am trying to achieve an effect in an application I am writing and maybe you can help.
The view hierarchy is reasonably complicated at this point so for simplicity let’s just say I have a UILabel with it’s Alpha value set to 0. Currently I’m using a UILongPressGestureRecognizer which is after a couple of seconds, calling a method that updates the text of the UILabel and executes a UIView Animation block to display it nicely (set's the Alpha back to 1 over the course of 1 second - fading it in).
This Block fades in the newly updated label, then in it’s completion block fades it back out over the course of another second.
This is OK, but what I really want is to have the label fade in (via UIView Animations or whatever) after a long press (say 3 seconds). Then stay on screen while the press is still active, then fade out a second after the touch is lifted. How can I achieve this effect?
Should I forget about gesture recognizers and move to UIView animation blocks and pole the various touch up touch down states?
Thanks for reading, Regards,
John
Upvotes: 0
Views: 379
Reputation: 4485
Every UIGestureRecognizer have state. When handle long press, just check state.
- (void)handleLongPress:(UILongPressGestureRecognizer*)longPress
{
if(longPress.state == UIGestureRecognizerStateEnded){
//do what you want
NSLog(@"%@", @"end");
}
}
Upvotes: 1
Reputation: 119041
The long press recogniser will work, you just need to check the state to know what to do. When the gesture is first recognised, start the fade in. When the gesture is completed, start the fade out, but using the method which allows you to set the start delay.
- (void)handleLongPress:(UILongPressGestureRecognizer*)sender
{
if (sender.state == UIGestureRecognizerStateEnded) {
// fade out, delayed
}
else if (sender.state == UIGestureRecognizerStateBegan) {
// fade in
}
}
Upvotes: 1