CALayer&Core Animation核心动画
来源:互联网 发布:python调用caffe测试 编辑:程序博客网 时间:2024/06/05 15:08
CALayer和UIView的关系
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. 如果一个动画不需要于用户交互,通常就使用核心动画,转场动画一般都使用核心动画
1 0
- CALayer&Core Animation核心动画
- Core Animation CALayer动画
- 核心动画Core Animation
- Core Animation 核心动画
- 核心动画(Core Animation)
- 核心动画 Core Animation
- 核心动画(Core Animation)
- Core Animation核心动画
- Core Animation - 核心动画
- 核心动画Core Animation
- Core Animation(核心动画)
- ios CALayer Core Animation UIView动画封装
- IOS核心动画 - Core Animation
- 86-Core Animation 核心动画
- iOS-Core Animation 核心动画
- 核心动画(Core Animation)
- IOS核心动画(Core Animation)
- iOS Core Animation 核心动画
- POJ 1655 (树dp or 树重心)
- kali 2.0中metasploit的使用教程
- Universal-Image-Loader源码阅读(8)-core/imageaware/NonViewAware
- poj1328 贪心
- 2016.8.15 夏令营 Day15
- CALayer&Core Animation核心动画
- Picasso获取得到的Bitmap
- codeforces 510.B Fox And Two Dots (DFS好题)
- python数据持久存储:pickle模块的基本使用
- javacv 颜色识别&&拍照&&剪裁&&图像旋转
- 2016年8月15生活总结
- JavaScript特效——开关灯泡
- effective C++ 学习笔记 实现&&继承与面向对象设计
- 【CodeForces】510C - Fox And Names(拓扑 & STL)