四、CAKeyframeAnimation (针动画)
CAKeyframeAnimation (针动画)
怎么理解这个(针动画)呢,举个例子给大家:
一个view,我们想它从点A移动到点B,再移动到点C,整一个动画就是针动画
它是根据你指定的一系路径去执行动画的,也就是一针一针的执行动画,要实现这些针动画效果,CATransition,CABaseAnimation是实现不了的。
下面给个代码小例子去体会下,大家可直接copy运行下:
//
// ViewController.m
// KeyFrameAnimation
//
// Created by vincent_guo on 13-9-6.
// Copyright (c) 2013年 vincent_guo. All rights reserved.
//
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
@interface ViewController (){
UIImageView *_imageView;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_imageView = [[UIImageView alloc] init];
_imageView.bounds = CGRectMake(0, 0, 80, 80);
_imageView.center = CGPointMake(160, 50);
_imageView.image = [UIImage imageNamed:@"clock"];
[self.view addSubview:_imageView];
[_imageView release];
}
-(void)click:(id)sender{
[self keyPathAnim];
}
#pragma mark 图片往下掉,再往上移动 看上去有反弹的效果
-(void)keyPathAnim{
//position 代表位置属性,如中心点(x,y)的值
CAKeyframeAnimation *positionAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
positionAnimation.duration = 0.5;
//定义个position(位置变换的路径)
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 160, 50);
CGPathAddLineToPoint(path, NULL, 160, 100);
CGPathAddLineToPoint(path, NULL, 160, 150);
CGPathAddLineToPoint(path, NULL, 160, 200);
CGPathAddLineToPoint(path, NULL, 160, 250);
CGPathAddLineToPoint(path, NULL, 160, 235);//为什么移动的y值又小了,是因为想让它实现反弹的效果
CGPathAddLineToPoint(path, NULL, 160, 250);
positionAnimation.path = path;
CGPathRelease(path);
[_imageView.layer addAnimation:positionAnimation forKey:@"keyframeanimation"];//开始动画
_imageView.center = CGPointMake(160, 250);//如果不写上这句,那么动画结束后,view返回到原来的位置
}
@end

浙公网安备 33010602011771号