CoreAnimations-> CAKeyframeAnimation(三)

CAKeyframeAnimation:关键帧动画

是通过某一属性按照一串的数值进行动画,就好像制作动画的时候一帧一帧的制作一样。

//•calculationMode目前提供如下几种模式:

//–kCAAnimationLinear 默认值,表示当关键帧为座标点的时候,关键帧之间直接直线相连进行插值计算

//–kCAAnimationDiscrete 离散的,不进行插值计算,所有关键帧直接逐个进行显示

//–kCAAnimationPaced 使得动画均匀进行,而不是按keyTimes设置的或者按关键帧平分时间,此时keyTimes和timingFunctions无效

//–kCAAnimationCubic 对关键帧为座标点的关键帧进行圆滑曲线相连后插值计算,这里的主要目的是使得运行的轨迹变得圆滑

//–kCAAnimationCubicPaced 看这个名字就知道和kCAAnimationCubic有一定联系,其实就是在kCAAnimationCubic的基础上使得动画运行变得均匀,就是系统时间内运动的距离相同,此时keyTimes以及timingFunctions也是无效的

//1 const kCAAnimationLinear//线性,默认

//2 const kCAAnimationDiscrete//离散,无中间过程,但keyTimes设置的时间依旧生效,物体跳跃地出现在各个关键帧上

//3 const kCAAnimationPaced//平均,keyTimes跟timeFunctions失效

//4 const kCAAnimationCubic//平均,同上

//5 const kCAAnimationCubicPaced//平均,同上



实例:

#import "JumpToKeyController.h"

@interface JumpToKeyController ()

@property (nonatomic, strong) UIView *viewRect;

@end

@implementation JumpToKeyController

#pragma mark - LfileCycle

- (void)viewDidLoad

{

[super viewDidLoad];

UIButton *pathButton = [UIButton createButton:@"path" target:self action:@selector(pathButtonClick)];

pathButton.frame = CGRectMake(80, 60, 100, 40);

[self.view addSubview:pathButton];

UIButton *zoomButton = [UIButton createButton:@"3D缩放效果" target:self action:@selector(zoomButtonClick)];

zoomButton.frame = CGRectMake(180, 60, 100, 40);

[self.view addSubview:zoomButton];

UIButton *certainButton = [UIButton createButton:@"移动" target:self action:@selector(certainButtonClik)];

certainButton.frame = CGRectMake(80, 100, 100, 40);

[self.view addSubview:certainButton];

self.viewRect = [[UIView alloc] initWithFrame:CGRectMake(40, 250, 50, 50)];

self.viewRect.backgroundColor = [UIColor redColor];

[self.view addSubview:_viewRect];

}

#pragma mark - Touch Event

- (void)pathButtonClick

{

//贝塞尔曲线

// 学习UIBezierPath:http://www.jianshu.com/p/734b34e82135

//    UIBezierPath *path = [UIBezierPath bezierPath];

//

//    [path moveToPoint:CGPointMake(-40, 100)];// 开始位置

//    [path addLineToPoint:CGPointMake(360, 100)];

//    [path addLineToPoint:CGPointMake(360, 200)];

//    [path addLineToPoint:CGPointMake(-40, 200)];

//    [path addLineToPoint:CGPointMake(-40, 300)];

//    [path addLineToPoint:CGPointMake(360, 300)];

// 使用画圆

// UIBezierPath这个类呢主要用于绘图。

UIBezierPath *path = [UIBezierPath bezierPath];

[path addArcWithCenter:self.view.center radius:100.0 startAngle:0.0 endAngle:180.0 clockwise:YES];

[path stroke];

CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];

animation.duration = 2.0f;

animation.rotationMode = kCAAnimationDiscrete;

animation.path = path.CGPath;

[animation setKeyTimes:[NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0],[NSNumber numberWithFloat:0.1], [NSNumber numberWithFloat:0.50],[NSNumber numberWithFloat:0.65], [NSNumber numberWithFloat:0.7],[NSNumber numberWithFloat:1.0], nil]];

[self.viewRect.layer addAnimation:animation forKey:@"moveAnimation"];

}

- (void)zoomButtonClick

{

CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];

///设置动画

CATransform3D forward = CATransform3DMakeScale(1.3, 1.3, 1);

CATransform3D back = CATransform3DMakeScale(0.7, 0.7, 1);

CATransform3D forward2 = CATransform3DMakeScale(1.2, 1.2, 1);

CATransform3D back2 = CATransform3DMakeScale(0.9, 0.9, 1);

[animation setValues:[NSArray arrayWithObjects:

[NSValue valueWithCATransform3D:CATransform3DIdentity],

[NSValue valueWithCATransform3D:forward],

[NSValue valueWithCATransform3D:back],

[NSValue valueWithCATransform3D:forward2],

[NSValue valueWithCATransform3D:back2],

[NSValue valueWithCATransform3D:CATransform3DIdentity],nil]];

[animation setDuration:1];// 执行动画时间

[self.viewRect.layer addAnimation:animation forKey:nil];

}

- (void)certainButtonClik

{

// 添加曲线Path

CGMutablePathRef path = CGPathCreateMutable();

// 开始位置

CGPathMoveToPoint(path, NULL, 50.f, 130.f);

// CG路径添加曲线点

CGPathAddCurveToPoint(path, NULL, 50.0, 275.0, 150.0, 275.0, 150.0, 120.0);

CGPathAddCurveToPoint(path,NULL,150.0,275.0,250.0,275.0,250.0,120.0);

CGPathAddCurveToPoint(path,NULL,250.0,275.0,350.0,275.0,350.0,120.0);

CGPathAddCurveToPoint(path,NULL,350.0,275.0,450.0,275.0,450.0,120.0);

CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];

[animation setPath:path];

[animation setDuration:3.0];

[animation setAutoreverses:YES];

[animation setRepeatCount:2.0];

CFRelease(path);

[self.viewRect.layer addAnimation:animation forKey:NULL];

}

@end

你可能感兴趣的:(CoreAnimations-> CAKeyframeAnimation(三))