iPhone - 使用 animateWithDuration 而不是 UIView beginAnimations 有什么优势吗?

发布于 2024-10-27 15:40:29 字数 447 浏览 0 评论 0原文

一个简单的问题:

这是一个老式动画的例子:

[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];

}];

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

应该有某种收获,否则苹果不会做出这个新功能。

你们怎么说?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

榕城若虚 2024-11-03 15:40:29

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

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

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

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.

And why to use beginAnimation: because you want to support versions of iOS prior to 4.0, where that code isn't available. 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.

情话已封尘 2024-11-03 15:40:29

我认为 animateWithDuration 更新并且看起来更好。我比 beginAnimation 使用它更多。这是更清晰的代码。 beginAnimation 当您需要兼容低于 4.0 的 iOS 版本时使用。

但在某些情况下,beginAnimation有更多优势,当你编写带有参数animated的函数时会更容易。示例:

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

I think animateWithDuration is newer and look better. I use it more than beginAnimation. It is more clear code. beginAnimation use when you need compatible for iOS version less than 4.0.

But in some case, beginAnimation has more advantage, make easier when you write a function with a parameter animated. 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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文