动画时设置属性两次

发布于 2024-11-24 04:03:02 字数 545 浏览 1 评论 0原文

如果我在动画块内将属性两次设置为两个不同的值,会发生什么情况?如果我执行以下伪代码:

myView.frame = CGRectMake(0,  0, 50, 50); // state 0
[UIView beginAnimations:@"showBanner" context:NULL];
{
    [UIView setAnimationDuration:2];
    myView.frame = CGRectMake(0, 20, 50, 50); // state 1
    myView.frame = CGRectMake(0, 10, 50, 50); // state 2
}
[UIView commitAnimations];

我应该得到以下哪个结果?

  1. 帧从状态 0 通过状态 1 到状态 2 进行动画处理。
  2. 帧从状态 0 直接进行动画处理到状态 2,忽略状态 1。

我希望结果 #2 发生,因为我认为属性的状态是在动画已提交。我的应用程序中出现的行为似乎表明结果#1 正在发生,因此我提出了问题。

What happens if I set a property twice to two different values inside of an animation block? If I execute the following pseudocode:

myView.frame = CGRectMake(0,  0, 50, 50); // state 0
[UIView beginAnimations:@"showBanner" context:NULL];
{
    [UIView setAnimationDuration:2];
    myView.frame = CGRectMake(0, 20, 50, 50); // state 1
    myView.frame = CGRectMake(0, 10, 50, 50); // state 2
}
[UIView commitAnimations];

which of the following results should I get?

  1. The frame animates from state 0 through state 1 to state 2.
  2. The frame animates from state 0 directly to state 2, ignoring state 1.

I would expect result #2 to happen because I would think that the state of the properties is recorded when the animation is committed. I'm getting a behavior in my app which seems to indicate that result #1 is happening, hence my question.

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

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

发布评论

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

评论(3

世态炎凉 2024-12-01 04:03:05

(2) 帧从状态 0 直接动画到状态 2,忽略状态 1。

(2) The frame animates from state 0 directly to state 2, ignoring state 1.

堇色安年 2024-12-01 04:03:05

我需要这个问题的答案,但没有找到令人满意的答案。我构建了一个快速测试,放置了一个像这样的小子视图:

- (void)viewDidLoad {

    [super viewDidLoad];

    self.testView = [[UIView alloc] initWithFrame:CGRectMake(0,0,40,40)];
    self.testView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.testView];
}

然后提供了两个像这样的替代动画:

- (void)animateWithSimpleBlock {

    [UIView animateWithDuration:2 animations:^{
        self.testView.frame = CGRectMake(200,200,40,40);
        self.testView.frame = CGRectMake(  0,300,40,40); 
    }];
}

- (void)animateWithQualifiedBlock {

    [UIView animateWithDuration:2 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        self.testView.frame = CGRectMake(200,200,40,40);
        self.testView.frame = CGRectMake(  0,300,40,40); 
    }];
}

发生的情况如下:在 animateWithSimpleBlock 上,testView 从初始状态跳转到单帧(无动画) 0,0 位置到中间位置 200,200。从那里开始,它会以超过 2 秒的动画时间到达最终的 0,300 位置。

但是 animateWithQualifiedBlock 可以将 testView 从最初的 0,0 位置平滑地动画到最终的 0,300 位置。这是我需要的行为(b/c 我正在使用动画块中调用的循环方法构建新的视图位置)。

I needed an answer to this question and didn't find either one given here satisfactory. I built a quick test that placed a small subview like this:

- (void)viewDidLoad {

    [super viewDidLoad];

    self.testView = [[UIView alloc] initWithFrame:CGRectMake(0,0,40,40)];
    self.testView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.testView];
}

And then provided two alternative animations like this:

- (void)animateWithSimpleBlock {

    [UIView animateWithDuration:2 animations:^{
        self.testView.frame = CGRectMake(200,200,40,40);
        self.testView.frame = CGRectMake(  0,300,40,40); 
    }];
}

- (void)animateWithQualifiedBlock {

    [UIView animateWithDuration:2 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        self.testView.frame = CGRectMake(200,200,40,40);
        self.testView.frame = CGRectMake(  0,300,40,40); 
    }];
}

Here's what happens: on animateWithSimpleBlock, the testView jumps in a single frame (no animation) from it's initial 0,0 position to the intermediate position 200,200. From there, it animates over 2sec to the final 0,300 position.

But animateWithQualifiedBlock animates the testView smoothly from it's initial 0,0 position to the final 0,300 position. This is the behavior I need (b/c I'm building up new view positions with looping methods called in the animation block).

空‖城人不在 2024-12-01 04:03:04

实际上答案是3。以上都不是。

动画块只会为属性的最后一次状态更改提供动画效果。因此,在您的情况下,框架将从状态 1 动画到状态 2。

Actually the answer is 3. None of the above.

The animation block will only animate the last state change for the property. So in your case the frame will animate from state 1 to state 2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文