HelmiB
HelmiB

Reputation: 12333

Can't touch during animation (Block Animation)

I have a View with UIButton, UITextField, and a UIImageView for Background.

in viewDidLoad i try animate UIImageView from alpha=0 to alpha =1 using block. it's pretty basic, here's the code:

[UIView animateWithDuration:1.5 animations:^(void){

    ((UIView*)[self.view viewWithTag:123]).alpha = 1;
}completion:^(BOOL finished){
}];

which is working fine. but during that 1.5 seconds of animation, my touch in current view seems to be disabled. i can't click any of the buttons or textFields until animation finish.

Thanks in Advance

Upvotes: 9

Views: 3787

Answers (6)

Ömer Sina Ceylan
Ömer Sina Ceylan

Reputation: 21

Isn’t it something that happens on standart iOS apps like Settings back button, menu swipe. There is no way to touch to screen while it is animating whereas you can touch in Android.

Upvotes: 0

Rafat touqir Rafsun
Rafat touqir Rafsun

Reputation: 2817

UIView userinteraction in animation using .allowUserInteraction for Swift 3+,iOS 10

Add to the animation call the UIViewAnimationOptions flag .allowUserInteraction

UIView.animate(withDuration: 1.0, delay: 0.0, usingSpringWithDamping: 0.4, initialSpringVelocity: 0.0, options: [.curveEaseIn,.allowUserInteraction], animations: {

            //your animation block

            }, completion: { isAnimated in
            //after animation is done
    })

Upvotes: 3

Bueno
Bueno

Reputation: 1950

Seems like animating buttons in general makes their click states pretty finicky. The solution I came up with for this was to have the animating element just be a UIView with all the button styles. Before the UIView animates I add a real button to the view above the UIView with a clear background to the location I want the user interaction to occur. Definitely adds an extra step but is very dependable.

//create button and view
UIButton *viewNotificationBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, ([UIScreen mainScreen].bounds.size.height -70), 320, 50)];
UIView *viewNotificationView = [[UIView alloc] initWithFrame:CGRectMake(0, ([UIScreen mainScreen].bounds.size.height), 320, 50)];

//add elements to screen
[self.view addSubview:viewNotificationView];
[self.view insertSubview:viewNotificationBtn aboveSubview:viewNotificationView];


[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
    [viewNotificationView setFrame:CGRectMake(0, ([UIScreen mainScreen].bounds.size.height - 70), 320, 50)];
} completion:^(BOOL finished) {
    [UIView animateWithDuration:0.5 delay:10.0 options:UIViewAnimationOptionAllowUserInteraction animations:^{
        viewNotificationView.alpha = 0;
    } completion:^(BOOL finished) {
        [viewNotificationView removeFromSuperview];
        [viewNotificationBtn removeFromSuperview];
    }];
}];

Upvotes: 1

HelmiB
HelmiB

Reputation: 12333

I ended up not doing this. You can't touch a view that still animating. that's my conclusion. Would be happy to hear some thought on this though.

Upvotes: 0

d.lebedev
d.lebedev

Reputation: 2303

You should use option UIViewAnimationOptionAllowUserInteraction as in the following example:

[UIView animateWithDuration:1.0 
                      delay:0 
                    options:UIViewAnimationOptionAllowUserInteraction 
                 animations:^{ myView.alpha = 0.5; } 
                 completion:NULL];

Upvotes: 31

Martin Ullrich
Martin Ullrich

Reputation: 100581

Use animateWithDuration:delay:options:animations:completion: method and set UIViewAnimationOptionAllowUserInteraction as option

Upvotes: 0

Related Questions