您的位置:首页 > 移动开发 > IOS开发

iOS开发之关键帧动画

2016-01-08 23:28 369 查看
CABasicAnimation揭示了大多数隐式动画背后以来的机制,但是显示的给图层添加CABasicAnimation相较于隐式动画而言,之恩能够说是非礼不讨好。

CAKeyFrameAnimation是一种UIKit没有暴露出来但是功能强大的类。和CABasicAnimation类似,CAKeyFrameAnimation同样是CAPropertyAnimation的一个子类,它依然作用于单一的一个属性,但是和CABasicAnimation不一样的是,它不限制设置一个起始和结束的值,而是可以根据一连串随意地值来做动画。

关键帧起源于传动动画,意思是指主导的动画在显著改变发生时重绘当前帧,每帧之间剩下的绘制有系统来推算绘制完成 。CAKeyframeAnimation也是同样的道理:你提供了显著的帧(关键帧),剩下的由Core Animation在每帧之间插入。

#import "ViewController.h"
#define WIDTH [[UIScreen mainScreen].bounds.size.width]
#define HEIGHT [[UIScreen mainScreen].bounds.size.height]
@interface ViewController ()
@property (nonatomic,strong)UIView *containerView;
@property (nonatomic,strong)CALayer * colorLayer;
@end

@implementation ViewController

- (void)viewDidLoad {
[superviewDidLoad];
//关键帧动画
// [self customeLayer];
//贝塞尔曲线
[self createBezierPath];
}
- (void)createBezierPath{

self.containerView = [[UIViewalloc]initWithFrame:CGRectMake(10,100, 200,2000)];
[self.viewaddSubview:self.containerView];
//create a path
UIBezierPath * bezierPath = [[UIBezierPathalloc]init];
[bezierPath moveToPoint:CGPointMake(0,150)];
//绘制曲线
[bezierPath addCurveToPoint:CGPointMake(300,150) controlPoint1:CGPointMake(75,0) controlPoint2:CGPointMake(30,400)];
//draw the path using a CAShapeLayer

CAShapeLayer * pathLayer = [CAShapeLayerlayer];
pathLayer.path = bezierPath.CGPath;
//设置填充色
pathLayer.fillColor = [UIColorwhiteColor].CGColor;
//设置线条色
pathLayer.strokeColor = [UIColorredColor].CGColor;
pathLayer.lineWidth =3.0f;
[self.containerView.layeraddSublayer:pathLayer];

//add the ship
CALayer * shipLayer = [CALayerlayer];
shipLayer.frame =CGRectMake(0,0, 64,64);
shipLayer.position =CGPointMake(0,150);
shipLayer.contents = (__bridgeid)[UIImageimageNamed:@"选择框"].CGImage;
[self.containerView.layeraddSublayer:shipLayer];

//create the keyframe animation
CAKeyframeAnimation * animation = [CAKeyframeAnimationanimation];
animation.keyPath =@"position";
animation.duration =4.0f;
animation.path = bezierPath.CGPath;
//设置运动的方向
animation.rotationMode =kCAAnimationRotateAuto;
[shipLayer addAnimation:animationforKey:nil];

}
- (void)customeLayer{
self.colorLayer = [CALayerlayer];
self.colorLayer.frame =CGRectMake(100.f,100.f, 100.f,100.f);
self.colorLayer.backgroundColor = [UIColorblueColor].CGColor;
[self.view.layeraddSublayer:self.colorLayer];

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(),
^{
[selfCAKeyAnimation];
});
}
- (void)CAKeyAnimation{

CAKeyframeAnimation *animation = [CAKeyframeAnimationanimation];
animation.keyPath =@"backgroundColor";
animation.duration =2.0;
animation.values =@[(__bridgeid)[UIColorblueColor].CGColor,(__bridgeid)[UIColorredColor].CGColor,(__bridgeid)[UIColorgreenColor].CGColor,(__bridgeid)[UIColorblueColor].CGColor];

[self.colorLayeraddAnimation:animation forKey:nil];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: