Reputation: 1134
in my app I have some animations. for example I have a button in my main menu and when you click it animation begins (like moving some place etc.) and at the end of the animation it is navigated to an another page. What I need is disabling the user interaction during the animation. because during the animation If I press the starting point of my button, the page which is supposed to be navigated is opened twice. To sum up, If I do not let any kind of user interaction during the animation, my problem will be solved. How can I do that?
Upvotes: 13
Views: 10294
Reputation: 2287
I had view controller with icons that open pages. If the user was tapping quickly icon1 and icon2, the 2 pages opened.
to prevent that I had this 2 lines to the beginning of the tap event this make sure the whatever happen, the endIgnoring will call
-(void) on_image_tap:(UITapGestureRecognizer * ) tapGesture
{
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[[UIApplication sharedApplication] performSelector:@selector(endIgnoringInteractionEvents) withObject:nil afterDelay:0.5f];
Upvotes: 0
Reputation: 2285
This might help:
// for ignoring event
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
Code will look like:
[UIView animateWithDuration:1.0 animations:^{
//some animation
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
}
completion:^(BOOL done){
if (done){
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
}
}
];
Upvotes: 18
Reputation:
You don't have to hack around with the completion block - there's an animation option which does just this exactly:
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction
animations:^{
// animations here
}
completion:nil];
If you had set the UIViewAnimationOptionAllowUserInteraction
, then user interaction would have been allowed.
Upvotes: 5
Reputation: 230
To disable touch event in a view,
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
To enable touch event in a view
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
Upvotes: 2
Reputation: 31081
Disable userIntrection of Button.
Btn.userInteractionEnabled = NO;
Upvotes: 1
Reputation: 441
Before animation:
self.view.userInteractionEnabled = NO;
and in animation completion block:
self.view.userInteractionEnabled = YES;
Upvotes: 23
Reputation: 3921
yourView.userInteractionEnabled = NO;
[UIView animateWithDuration:1 animations:^
{
//animations here
}
completion:^(BOOL finished)
{
yourView.userInteractionEnabled = YES;
}];
Upvotes: 2
Reputation: 130193
Simple, you can set setUserInteractionEnabled
to NO
before the animation starts, and in the animations completion handler set it back to YES
.
[myObject setUserInteractionEnabled:NO];
[UIView animateWithDuration:1.0 animations:^{
[myObject setTransform:CGAffineTransformMakeTranslation(100, 100)];//some animation
}completion:^(BOOL done){
if (done){
[myObject setUserInteractionEnabled:YES];
}
}];
Upvotes: 6