OptionalBlockAnimation 1.0.0

OptionalBlockAnimation 1.0.0

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布时间最后发布2014年12月

Adam Iredale 维护。



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

  • 如果 animateYES,则您可以期望与调用普通方法相同的行为。
  • 如果 animateNO,则动画和完成块将像上面的第二个示例一样调用。

一些调用有一些小的变化,所以请随时查看代码。如往常一样,欢迎提交拉取请求。

要求

  • UIKit

安装

作者

Adam Iredale,@iosengineer on Twitter

许可证

OptionalBlockAnimation 可在 MIT 许可证下使用。有关更多信息,请参阅 LICENSE 文件。