线性动画。
初始化动画对象:
_imageView = [[UIImageView alloc]init];
[_imageView setBackgroundColor:[UIColor clearColor]];
[_imageView setFrame:CGRectMake(20, 40, 20, 60)];
[_imageView setImage:[UIImage imageNamed:@"brush.png"]];
[self.view addSubview:_imageView];
1.设置起点
- (void)createPath {
self.starPath = CGPathCreateMutable();
CGPathMoveToPoint(self.starPath, NULL, 40, 80);
}
if (self.starPath) {
CFRelease(self.starPath);
[self createPath];
[self.view.layer setNeedsDisplay];
}
CGMutablePathRef path = CGPathCreateMutable(); /* 初始化创建路线 */
CGPathMoveToPoint(path, NULL, 40, 80); /* 路线起点 */
CGPathAddLineToPoint(path, NULL, 80, 150); /* 连线到第二个点*/
CGPathAddLineToPoint(path, NULL, 100, 90); /* 连线到第三个点*/
CGPathCloseSubpath(path); /* 路线close 起来,即让第三个点和第一点自动连接*/
CAKeyframeAnimation * theAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
theAnimation.path = path; /* 动画路线 */
theAnimation.duration = 10 ; /* 动画总时间*/
CFRelease(path);
3.选择动画图层:
[self.imageView.layer addAnimation:theAnimation forKey:@"position"]; /* 让图片开始动画 */