简体   繁体   English

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

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

One simple question: 一个简单的问题:

This is an example of an old fashion animation: 这是一个老式时尚动画的示例:

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

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

[UIView commitAnimations];

this can be written like 这可以写成

[UIView animateWithDuration:0.5 animations:^{

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

}];

is there any advantage in rewriting the animation using this new form? 使用这种新形式重写动画有什么优势吗?

There should be some kind of gain, or Apple would not make this new function. 应该会有一些收获,否则苹果将无法实现这一新功能。

What do you guys say? 你们怎么说

Apple made the change not for performance, but because blocks are an easier way to express this kind of thing. 苹果所做的改变不是为了性能,而是因为块是表达这种事情的一种更简单的方法。 Previously you'd have to use selectors to be triggered when an animation finished, etc. 以前,您必须使用选择器在动画结束时触发等。

So - why to use animateWithDuration : because blocks save time, make code cleaner, and are just generally very useful. 所以-为什么要使用animateWithDuration :因为块节省了时间,使代码更整洁,并且通常非常有用。

And why to use beginAnimation : because you want to support versions of iOS prior to 4.0, where that code isn't available. 以及为什么要使用beginAnimation :因为您要支持4.0之前的iOS版本,因此该代码不可用。 Apple still need to provide both methods because they need to remain backwards compatible - but the documentation strongly recommends you use the blocks version of methods where available and appropriate. Apple仍需要提供这两种方法,因为它们需要保持向后兼容-但是文档强烈建议您在可用且适当的地方使用方法的块版本。

I think animateWithDuration is newer and look better. 我认为animateWithDuration较新并且外观更好。 I use it more than beginAnimation . 我使用它的方式比beginAnimation多 It is more clear code. 它是更清晰的代码。 beginAnimation use when you need compatible for iOS version less than 4.0. 当您需要与低于4.0的iOS版本兼容时使用beginAnimation

But in some case, beginAnimation has more advantage , make easier when you write a function with a parameter animated . 但在某些情况下,beginAnimation有更多的优势 ,能够更方便,当你写与动画参数的函数。 Example: 例:

- (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
}

Instead of: 代替:

- (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