TechNote 9 – Animating Views
Animating views in iOS using blocks is a great way to simplify your code. However, the current API has one drawback; the default is to disable user interaction during the animation. e.g.
The “old” way, pre-blocks
[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:HIDE_DURATION]; [self _hide]; [UIView commitAnimations];
With Block:
[UIView animateWithDuration:HIDE_DURATION animations:^{ [self _hide]; }];
Much neater, but as I mentioned, with blocks user interaction is disabled by default (whereas it wasn’t in the “old” way). To enable user interaction, you need to supply the UIViewAnimationOptionAllowUserInteraction option (plus the default ones of UIViewAnimationOptionCurveEaseInOut and UIViewAnimationOptionTransitionNone), but then you can’t use any of the shortcut methods:
With Blocks and User Interaction enabled:
[UIView animateWithDuration:HIDE_DURATION
delay:0
options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionTransitionNone
animations:^{ [self _hide]; }
completion:^(BOOL finished) {}
];
This is too much unnecessary code for me (i.e. the empty completion block), so I decided to create a simple category that enables user interaction by default:
UIView+InteractiveAnimation.h
+ (void)animateWithInteractiveDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion; + (void)animateWithInteractiveDuration:(NSTimeInterval)duration animations:(void (^)(void))animations;
UIView+InteractiveAnimation.m
+ (void)animateWithInteractiveDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion {
[UIView animateWithDuration:duration
delay:0
options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionTransitionNone
animations:animations
completion:completion
];
}
+ (void)animateWithInteractiveDuration:(NSTimeInterval)duration animations:(void (^)(void))animations {
[UIView animateWithDuration:duration
delay:0
options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionTransitionNone
animations:animations
completion:^(BOOL finished) {}
];
}
So now I can have clean animation code, with user interactions:
With Blocks and User Interactions using UIView+InteractiveAnimation category:
[UIView animateWithInteractiveDuration:HIDE_DURATION animations:^{ [self _hide]; }];
Enjoy…

