11
votes

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?

8
How do you animate the view? Traditional method or using blocks? - user529758
@NSPostWhenIdle..why is it wrong or stackOverflow not allow it? - Rajneesh071
ok sir...next time i will l care for it,...:) - Rajneesh071
@death - if you have to button in a view and want to animation on both button simultaneously then? - Rajneesh071

8 Answers

21
votes

Before animation:

self.view.userInteractionEnabled = NO;

and in animation completion block:

self.view.userInteractionEnabled = YES;
18
votes

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]; 
        }
    }
];
6
votes

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];
    }
}];
5
votes

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.

2
votes
yourView.userInteractionEnabled = NO;
[UIView animateWithDuration:1 animations:^
{
    //animations here                    
}
completion:^(BOOL finished)
{
    yourView.userInteractionEnabled = YES;
}];
2
votes

To disable touch event in a view,

 [[UIApplication sharedApplication] beginIgnoringInteractionEvents];

To enable touch event in a view

[[UIApplication sharedApplication] endIgnoringInteractionEvents];
1
votes

Disable userIntrection of Button.

Btn.userInteractionEnabled = NO;
0
votes

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];