CALayer&Core Animation核心动画
2016-08-15 17:04
351 查看
CALayer和UIView的关系
CALayer负责视图中显示的内容和动画
UIView负责监听和响应事件
UIView本身不具备显示的功能,是它内部的层才有显示功能
非根层layer的隐式动画
layer的形变 可通过KVC来快速变形
自定义Layer
注:
1.CALayer是定义在QuartzCore框架中的(Core Animation)
2.CGImageRef、CGColorRef两种数据类型是定义在CoreGraphics框架中的
3.UIColor、UIImage是定义在UIKit框架中的
QuartzCore框架和CoreGraphics框架是可以跨平台使用的,在iOS和Mac OS X上都能使用
但是UIKit只能在iOS中使用
为了保证可移植性,QuartzCore不能使用UIImage、UIColor,只能使用CGImageRef、CGColorRef
1.Mac OS iOS 跨平台
2.动画执行过程在后台操作 ,不会阻塞线程
3.是作用在CAlayer上的
使用方法
CABasicAnimation
CAKeyframeAnimation
转场动画CATransition
动画组CAAnimationGroup
Animation代理
注:
1. 核心动画一些都是假象,并不能真实的改变属性的值
2. 如果一个动画不需要于用户交互,通常就使用核心动画,转场动画一般都使用核心动画
CALayer负责视图中显示的内容和动画
UIView负责监听和响应事件
UIView本身不具备显示的功能,是它内部的层才有显示功能
//创建图层 CALayer *layer = [CALayer layer]; layer.backgroundColor = [UIColor redColor].CGColor; layer.frame = CGRectMake(50, 50, 100, 100); // 设置layer的内容 layer.contents = (id)[UIImage imageNamed:@"图片"].CGImage; // 添加到控制器的View的layer上面 [self.view.layer addSublayer:layer]; // 设置阴影 Opacity:不透明度 NO:透明 layer.shadowOpacity = 1; //阴影偏移量 layer.shadowOffset = CGSizeMake(-20, -20); //阴影圆角 layer.shadowRadius = 30; // UIKit -> CoreGraphics .CG layer.shadowColor = [UIColor yellowColor].CGColor; // 设置圆角半径 layer.cornerRadius = 50; // 超出主层的部分全部裁剪掉 layer.masksToBounds = YES; // 设置边框 layer.borderWidth = 3; layer.borderColor = [UIColor whiteColor].CGColor;
//用来设置CALayer在父层中的位置 //以父层的左上角为原点(0, 0) @property CGPoint position; //“锚点”决定着CALayer身上的哪个点会在position属性所指的位置 //它的x、y取值范围都是0~1,默认值为(0.5, 0.5) @property CGPoint anchorPoint; 注: 如果默认设置bounds属性 则position默认为(0,0);
非根层layer的隐式动画
// 默认的View的layer是根层,没有隐式动画 通过改变layer的 position cornerRadius borderWidth backgroundColor borderColor 属性 会进行隐式动画 //默认每个动画都会包装到一个事务 // 执行隐式动画之前去设置这个动画时长 [CATransaction setAnimationDuration:1]; //禁止隐式动画 [CATransaction beign] [CATransaction setDisableActions:Yes] [CATransaction commit]
layer的形变 可通过KVC来快速变形
//缩放 [self.redView.layer setValue:@0.5 forKeyPath:@"transform.scale"]; //旋转 [self.redView.layer setValue:@M_PI forKeyPath:@"transform.rotation.y"];
自定义Layer
setNeedsDisplay方法 底层调用 - (void)drawInContext:(CGContextRef)ctx; 设置代理后 调用 - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx ; (两个同时实现 最终呈现的是drawInContext方法的效果)
注:
1.CALayer是定义在QuartzCore框架中的(Core Animation)
2.CGImageRef、CGColorRef两种数据类型是定义在CoreGraphics框架中的
3.UIColor、UIImage是定义在UIKit框架中的
QuartzCore框架和CoreGraphics框架是可以跨平台使用的,在iOS和Mac OS X上都能使用
但是UIKit只能在iOS中使用
为了保证可移植性,QuartzCore不能使用UIImage、UIColor,只能使用CGImageRef、CGColorRef
CoreAnimation
1.Mac OS iOS 跨平台
2.动画执行过程在后台操作 ,不会阻塞线程
3.是作用在CAlayer上的
使用方法
CABasicAnimation
CABasicAnimation *anim = [CABasicAnimation animation]; // 更改layer的哪个属性进行核心动画, anim.keyPath = @"transform.scale"; // 改变什么样的值 anim.toValue = @0.5;; // 取消反弹 // 在动画执行完毕的时候不要移除动画 anim.removedOnCompletion = NO; // 保持最新的模式 anim.fillMode = kCAFillModeForwards; // 设置动画的执行次数,MAXFLOAT最大的执行次数 anim.repeatCount = MAXFLOAT; [self.layer addAnimation:anim forKey:nil];
CAKeyframeAnimation
CAKeyframeAnimation *anim = [CAKeyframeAnimation animation]; // 设置动画属性 anim.keyPath = @"transform.rotation"; // 设置动画的值 anim.values = @[@(-5 / 180.0 *M_PI),@(5 / 180.0 *M_PI)]; anim.duration = 2; // 自动反转 anim.autoreverses = YES; // 设置动画次数 anim.repeatCount = MAXFLOAT; [self.iconView.layer addAnimation:anim forKey:nil];
转场动画CATransition
// 注意点:过渡代码一定要和转场代码放在一起 CATransition *anim = [CATransition animation]; anim.type = @"cube"; anim.subtype = kCATransitionFromTop; anim.duration = 2; // 设置动画进度 // anim.startProgress = 0.5; [self.imageView.layer addAnimation:anim forKey:nil];
动画组CAAnimationGroup
CAAnimationGroup *group = [CAAnimationGroup animation]; // 平移 CABasicAnimation *animT = [CABasicAnimation animation]; animT.keyPath = @"transform.translation"; NSValue *ponitV = [NSValue valueWithCGPoint:CGPointMake(50, 50)]; // 在原来的基础上偏移位置 animT.byValue = ponitV; // 缩放 CABasicAnimation *animS = [CABasicAnimation animation]; animS.keyPath = @"transform.scale"; animS.toValue = @0.5; // 旋转 CABasicAnimation *animR = [CABasicAnimation animation]; animR.keyPath = @"transform.rotation"; animR.toValue = @(M_PI); // 同时做很多动画 group.animations = @[animT,animS,animR]; // 取消反弹 group.removedOnCompletion = NO; group.fillMode = kCAFillModeForwards; [self.imageView.layer addAnimation:group forKey:nil];
Animation代理
//不用遵守任何协议 只需要设置代理 anim.delegate = self; // 分类也可以叫做非正式协议 - (void)animationDidStart:(CAAnimation *)anim; - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag;
注:
1. 核心动画一些都是假象,并不能真实的改变属性的值
2. 如果一个动画不需要于用户交互,通常就使用核心动画,转场动画一般都使用核心动画
相关文章推荐
- iOS 动画效果:Core Animation & Facebook's pop
- iOS开发-CALayer/Core Animation/UIView动画封装
- iOS 动画效果:Core Animation & Facebook
- iOS开发基础知识:Core Animation(核心动画)
- iOS开发基础知识:Core Animation(核心动画)
- IOS学习笔记 CALayer & 核心动画 CALayer基本属性的使用(1)
- iOS开发基础知识:Core Animation(核心动画)
- 核心动画编程指南Core Animation Programming Guide - iOS
- IOS CoreAnimation 核心动画解析
- iOS开发基础知识:Core Animation(核心动画)
- iOS开发基础知识:Core Animation(核心动画)
- 核心动画渲染架构(Core Animation Rendering Architecture)
- iOS Core Animation(核心动画)
- ios-day18-06(核心动画Core Animation简介、CAAnimation属性介绍)
- iOS 动画效果:Core Animation & Facebook's pop
- iOS开发基础知识:Core Animation(核心动画) 详解
- iOS学习笔记09-核心动画CoreAnimation
- iOS开发基础知识:Core Animation(核心动画)
- iOSCore Animation(核心动画)
- iOS开发基础知识:Core Animation(核心动画)