繁体   English   中英

iPhone-使用animateWithDuration代替UIView beginAnimations有任何优势吗?

[英]iPhone - is there any advantage in using animateWithDuration instead of UIView beginAnimations?

一个简单的问题:

这是一个老式时尚动画的示例:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];

[base setTransform:rotate];
[base setCenter:newCenter];

[UIView commitAnimations];

这可以写成

[UIView animateWithDuration:0.5 animations:^{

[base setTransform:rotate];
[base setCenter:newCenter];

}];

使用这种新形式重写动画有什么优势吗?

应该会有一些收获,否则苹果将无法实现这一新功能。

你们怎么说

苹果所做的改变不是为了性能,而是因为块是表达这种事情的一种更简单的方法。 以前,您必须使用选择器在动画结束时触发等。

所以-为什么要使用animateWithDuration :因为块节省了时间,使代码更整洁,并且通常非常有用。

以及为什么要使用beginAnimation :因为您要支持4.0之前的iOS版本,因此该代码不可用。 Apple仍需要提供这两种方法,因为它们需要保持向后兼容-但是文档强烈建议您在可用且适当的地方使用方法的块版本。

我认为animateWithDuration较新并且外观更好。 我使用它的方式比beginAnimation多 它是更清晰的代码。 当您需要与低于4.0的iOS版本兼容时使用beginAnimation

但在某些情况下,beginAnimation有更多的优势 ,能够更方便,当你写与动画参数的函数。 例:

- (void)moveSomethingWithAnimated:(BOOL)animated {

    // Do other task 1

    if( animated ) {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.2];

        someView.frame = newFrame;
        otherView.frame = newFrame;
    }

    if( animated ) {
        [UIView commitAnimations];
    }

    // Do other task 2
}

代替:

- (void)moveSomethingWithAnimated:(BOOL)animated {

    // Do other task 1

    if( animated ) {
        [UIView animateWithDuration:0.2 animations:^{
            someView.frame = newFrame;
            otherView.frame = newFrame;
        }];
    }
    else {
        // duplicate code, or you have to write another function for these two line bellow
        someView.frame = newFrame;
        otherView.frame = newFrame;
    }

    // Do other task 2
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM