block 动画的便捷扩展
您有多少次编写过这样的代码?
- (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
}
}
也许,就像我一样,您已经以一个更整洁的方式做了这件事。
- (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);
}
}
好吧,这可能更像是一个绕过问题的方式,而不是一个解决方案,但这确实让问题变得更整洁。
- (void)viewWillAppear:(BOOL)animated
{
[UIView OBA_animate:animated withDuration:0.25 animations:^
{
// Some manipulation
}
completion:^void(BOOL didFinish)
{
// Some completion action
}];
}
UIView (UIViewAnimationWithBlocks)
中的所有类方法都扩展了将 animate
作为布尔参数的 animate
。
animate
是 YES
,则您可以期望与调用普通方法相同的行为。animate
是 NO
,则动画和完成块将像上面的第二个示例一样调用。一些调用有一些小的变化,所以请随时查看代码。如往常一样,欢迎提交拉取请求。
Adam Iredale,@iosengineer on Twitter
OptionalBlockAnimation 可在 MIT 许可证下使用。有关更多信息,请参阅 LICENSE 文件。