我有这个代码:
当图像将 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];}
最佳答案
两个动画都在同一个运行周期中运行(这就是 UIView 动画的工作方式——一个运行周期中的所有动画都是“串联的”。您需要使用
[UIView setAnimationDelay:3.0];
关于第二个动画
示例代码
有两种方法可以做到这一点——使用标准的开始/提交动画,或使用 blocks 方法。此示例使用开始/提交动画代码,这就是您所拥有的。问题是因为动画被串联起来,并且在不进入 CA 的情况下,标准动画行为是一个“强制”在另一个运行之前结束。它与运行循环有关; 构建动画驱动的界面 来自 WWDC 2010深入探讨这一点。但是执行您想要的操作的代码如下所示:
- (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];
}
基本上,您必须强制它中断运行循环。要记住的关键是动画不会像您期望的代码那样按顺序执行。
基于 block 的代码如下所示。请注意,我使用的是 autorepeat 选项,它会自动重复播放动画。请注意,虽然在动画中,您正在设置一个属性,因此默认情况下,动画完成后 View 将恢复可见。因此,您必须在完成 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;}];
}
希望这对您有所帮助!
关于IOS: UIViewImage setAlpha 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6997659/