Objective-C 实现简单动画的步骤:
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) CALayer *ALayer; //定义一个名为ALayer的图层
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.ALayer = [CALayer layer]; //初始化
self.ALayer.bounds = CGRectMake(50, 50, 100, 100); //设置ALayer的边界
//self.ALayer.position = CGPointMake(0, 0); //设置起始位置
self.ALayer.backgroundColor = [UIColor blackColor].CGColor; //设置背景颜色
[self.view.layer addSublayer:self.ALayer]; //显示ALayer
[self positionAnimate]; //显示动画
}
- (void)positionAnimate
{
CAKeyframeAnimation *positionChange = [CAKeyframeAnimation animationWithKeyPath:@"position"]; //使用CAKeyframeAnimation建立一个有关键帧的移动动画
CGMutablePathRef path2 = CGPathCreateMutable(); //创建一个可改变路径path2
CGPathMoveToPoint(path2, NULL, 0, 0); //设置起点为0,0
//CGPathAddQuadCurveToPoint(path2,NULL,100,100,300,300); //
CGPathAddLineToPoint(path2, NULL , 300, 0); //设置路径横向移动300
positionChange.path = path2; //将path2设置为动画路径
//positionChange.delegate = self;
CGPathRelease(path2); //释放路径
positionChange.duration = 1; //设置动画速度为1
[self.ALayer addAnimation:positionChange forKey:nil]; //将动画添加进ALayer
}
参考资料:https://blog.csdn.net/sinat_31177681/article/details/51116413