微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

objective-c – 不鼓励使用beginAnimment

最近我被meronix通知使用beginAnimations是不鼓励的.通过UIView类参考阅读我看到这是真的 – 根据苹果类ref:

Use of this method is discouraged in iOS 4.0 and later. You should use
the block-based animation methods to specify your animations instead.

我看到大量的其他方法 – 我经常使用的 – 也是“不鼓励”,这意味着他们将在iOS 6(希望),但可能最终将被弃用/删除.

为什么这些方法不鼓励?

作为附注,现在我在各种应用程序中使用beginAnimations,最常见的是在显示键盘时移动视图.

//Pushes the view up if one of the table forms is selected for editing
- (void) keyboardDidShow:(NSNotification *)aNotification
{
    if ([isRaised boolValue] == NO)
    {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.25];
        self.view.center = CGPointMake(self.view.center.x,self.view.center.y-moveAmount);
        [UIView commitAnimations];
        isRaised = [NSNumber numberWithBool:YES];
    }
}

不确定如何使用基于块的方法来复制此功能;教程链接会很好

解决方法

因为有一个更好,更干净的选择,他们是不鼓励的

在这种情况下,所有块动画都会自动将您的动画更改(例如setCenter)封装在开始和提交调用中,以便您不要忘记.它还提供了一个完成块,这意味着你不必处理委托方法.

苹果公司的文档非常好,但作为一个例子,以块的形式做同样的动画

[UIView animateWithDuration:0.25 animations:^{
    self.view.center = CGPointMake(self.view.center.x,self.view.center.y-moveAmount);
} completion:^(BOOL finished){
}];

还有ray wenderlich有一个很好的帖子块动画:link

另一种方法是考虑块动画的可能实现

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations
{
    [UIView beginAnimations];
    [UIView setAnimationDuration:duration];
    animations();
    [UIView commitAnimations];
}

原文地址:https://www.jb51.cc/c/111714.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐