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];
}
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];
}
相关文章推荐
- 各种品牌进入Bios方式
- IOS数据存储
- iOS数据存储的三种方式
- iOS socket编程
- iOS开发 学习计划图
- iOS:使用block进行传值
- iOS学习中cocoapods的简单运用总结
- iOS常用手势
- iOS容易造成循环引用的三种场景,就在你我身边!
- MAC键盘快捷键
- iOS 学习--网页视图知识点
- 给你的iOS应用更换主题
- ios中协议与委托形象解释
- iOS中时间日期的基本应用
- iOS 史上最全的图片压缩方法集合
- iOS:点击button卡死
- iOS 如何做扫码安装
- IOS中的block和retain cycle (经典)
- 【iOS】Quartz 2D绘制直线/长方形/椭圆/圆形
- 在IOS 模拟器中 输入中文