IOS:UIViewImage setAlpha问题

发布于 2024-11-28 19:40:44 字数 395 浏览 2 评论 0原文

我有这样的代码:

当图像将 alpha 从 0.00 更改为 1.00 时,它会立即发生,而不是在 3 秒内发生,为什么?

- (void) startAnimation{

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:3];
[successView setAlpha:1.00];
[UIView commitAnimations];


[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:3];
[successView setAlpha:0.00];
[UIView commitAnimations];}

I have this code:

when image change alpha from 0.00 to 1.00 it is imediately and not in 3 seconds, why?

- (void) startAnimation{

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:3];
[successView setAlpha:1.00];
[UIView commitAnimations];


[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:3];
[successView setAlpha:0.00];
[UIView commitAnimations];}

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

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

发布评论

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

评论(1

拥抱没勇气 2024-12-05 19:40:44

两个动画都在同一个运行周期中运行(这就是 UIView 动画的工作原理 - 一个运行周期中的所有动画都是“串联的”。 您需要

[UIView setAnimationDelay:3.0];

在第二个动画上使用 罢工>


Example Code
There are two ways to do this—using the standard begin/commit animations, or using the blocks method. This example uses the begin/commit animations code, which is what you have. The issue is because the animations are being concatenated, and without going into CA, the standard animation behavior is for one to be "forced" to end before the other runs. It has to do with run loops; Building Animation-Driven Interfaces from WWDC 2010 goes into depth about this. But the code to do what you want looks like this:

- (void)fadeOut {
    [UIView beginAnimations:@"Animation2" context:NULL];
    [UIView setAnimationDuration:3];
    [self.testView setAlpha:0.00];
    [UIView commitAnimations];
}
- (IBAction)animate {
    [UIView beginAnimations:@"Animation1" context:NULL];
    [UIView setAnimationDuration:3];
    [self.testView setAlpha:1.00];
    [UIView commitAnimations];

    [self performSelector:@selector(fadeOut) withObject:nil afterDelay:3.0];
}

基本上,你必须强制它打破运行循环。要记住的关键一点是,动画并不是像您可能期望的代码那样按顺序执行。

基于块的代码如下所示。请注意,我使用的是自动重复选项,它会自动重复动画。请注意,尽管在动画中,您正在设置一个属性,所以默认情况下,动画完成后视图将恢复可见。因此,您需要在完成块中再次将该属性设置为零。

- (IBAction)animate {
    [UIView transitionWithView:self.testView duration:3.0 options:UIViewAnimationOptionAutoreverse animations:^{self.testView.alpha = 1.0;} completion:^(BOOL finished) {self.testView.alpha = 0.0;}];
}

希望这有帮助!

Both animations are run in the same run cycle (that's how UIView animations work—all animations in one run cycle are "concatenated." You'll need to use

[UIView setAnimationDelay:3.0];

On the second animation.


Example Code
There are two ways to do this—using the standard begin/commit animations, or using the blocks method. This example uses the begin/commit animations code, which is what you have. The issue is because the animations are being concatenated, and without going into CA, the standard animation behavior is for one to be "forced" to end before the other runs. It has to do with run loops; Building Animation-Driven Interfaces from WWDC 2010 goes into depth about this. But the code to do what you want looks like this:

- (void)fadeOut {
    [UIView beginAnimations:@"Animation2" context:NULL];
    [UIView setAnimationDuration:3];
    [self.testView setAlpha:0.00];
    [UIView commitAnimations];
}
- (IBAction)animate {
    [UIView beginAnimations:@"Animation1" context:NULL];
    [UIView setAnimationDuration:3];
    [self.testView setAlpha:1.00];
    [UIView commitAnimations];

    [self performSelector:@selector(fadeOut) withObject:nil afterDelay:3.0];
}

You have to force it to break up the run loop, basically. The key thing to remember is that animations are not executed sequentially, as you might expect code to be.

The blocks-based code looks like this. Note that I'm using the autorepeat option, which automatically repeats the animation. Note though that in the animation, you are setting a property, so by default after the animation completes the view will go back to being visible. Therefore, you have the set the property again to zero in the completion block.

- (IBAction)animate {
    [UIView transitionWithView:self.testView duration:3.0 options:UIViewAnimationOptionAutoreverse animations:^{self.testView.alpha = 1.0;} completion:^(BOOL finished) {self.testView.alpha = 0.0;}];
}

Hopefully this helps!

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