TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Dec 2014 |
Maintained by Adam Iredale.
A convenient extension to block animation
How many times have you written code like this?
- (void)viewWillAppear:(BOOL)animated
{
if (animated)
{
[UIView animateWithDuration:0.25 animations:^
{
// Some manipulation
} completion:^
{
// Some completion action
}]
}
else
{
// The SAME manipulation
// The SAME completion action
}
}
Perhaps, like me, you've made the transition to doing this in a slightly neater way.
- (void)viewWillAppear:(BOOL)animated
{
void (^animations)() = ^
{
// Some manipulation
};
void (^completion)(BOOL) = ^void(BOOL didFinish)
{
// Some completion action
};
if (animated)
{
[UIView animateWithDuration:0.25 animations:animations completion:completion];
}
else
{
animations();
completion(YES);
}
}
Well, maybe it's more of a workaround than a solution, but it sure makes the problem a lot neater.
- (void)viewWillAppear:(BOOL)animated
{
[UIView OBA_animate:animated withDuration:0.25 animations:^
{
// Some manipulation
}
completion:^void(BOOL didFinish)
{
// Some completion action
}];
}
All class methods from UIView (UIViewAnimationWithBlocks)
have been extended to include animate
as a boolean argument.
animate
is YES
then you can expect the same behaviour as calling the ordinary method.animate
is NO
, then the animations and completion blocks are called just like the second example above. There are some minor variations for some calls, so feel free to peruse the code. As usual, pull requests are welcome.
Adam Iredale, @iosengineer on Twitter
OptionalBlockAnimation is available under the MIT license. See the LICENSE file for more info.