NSTimer 不是实时机制;它作为运行循环的一部分触发,不适合创建流畅、流畅的动画。但是,有一个 SDK 是 iPhone OS 的一部分,称为 Core Animation——它提供了一个函数库,用于执行图层和视图的平滑动画。这是一个相当不错的系列视频教程,您可能会发现它们很有用。还有一本名为Core Animation for Mac OS X and iPhone的优秀书籍,您可能会发现它很有用。
还有来自 Apple 的核心动画文档,描述了如何使用 CABasicAnimation 类为任何层或视图的属性设置动画。这是文档中显式图层动画的示例:
CABasicAnimation *theAnimation;
theAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"];
theAnimation.duration=3.0;
theAnimation.repeatCount=1;
theAnimation.autoreverses=NO;
theAnimation.fromValue=CATransform3DTranslate(0,0,0);
theAnimation.toValue=CATransform3DTranslate(20,20,0);
[theLayer addAnimation:theAnimation forKey:@"animateTransform"];