1

我需要实现一个非常简单的动画,我必须使用 Core Animation 来完成它。动画是在我单击一个按钮后,一个视图淡入 2 秒,10 秒后淡出,但我不知道如何用 Core Animation 做到这一点,我会很感激任何建议。

4

1 回答 1

1

Core Animation Programming Guide 有一个非常好的基本示例,说明您想要使用显式 CABasicAnimation 对象做什么:

theAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"];
theAnimation.duration=2.0;
theAnimation.fromValue=[NSNumber numberWithFloat:0.0];
theAnimation.toValue=[NSNumber numberWithFloat:1.0];
[theLayer addAnimation:theAnimation forKey:@"animateOpacity"];

至于您想要的 10 秒延迟,您可以使用 GCD 等待 10 秒,然后反向执行完全相同的操作:

dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, 10000000000); // 10 seconds
dispatch_after(time, dispatch_get_main_queue(), ^()
{
    // Same thing, but with the fromValue/toValue reversed
});

**编辑:修复了 fromValue/toValue 值

于 2013-03-06T00:45:40.377 回答