iOS CALayer简单学习
2015-11-03 20:23
453 查看
最近突然想起写一点博客 今天写一点关于iOS图层的知识
在iOS中所有的View都是都自带一个layer View的所有内容也都是通过layer显现出来的 layer与图层的区别就是layer不能相应用户点击事件,而View可以响应用户点击事件,但layer更加轻量级,更加节省空间和迅速 下面我们就看一下吧
layer有几个比较重要的属性 : transform anchorPoint position
创建一个layerCALayer * layer = [[CALayer alloc]init] ; //CALayer * layer = [CALayer layer] ; // 边框宽度 layer.borderWidth = 10; // 边框颜色 layer.borderColor = [UIColor greenColor].CGColor; // 圆角 layer.cornerRadius = 10; //设置是否按照边界裁剪,如果设为yes,阴影就不能显现相出来 // layer.masksToBounds = YES; // 阴影颜色 layer.shadowColor = [UIColor blueColor].CGColor; // 阴影偏差 layer.shadowOffset = CGSizeMake(20, 20); // 阴影不透明度 layer.shadowOpacity = 0.5;
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo;">layer.<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">contents</span> = (<span style="font-variant-ligatures: no-common-ligatures; color: #c32275">id</span>)[<span style="font-variant-ligatures: no-common-ligatures; color: #6122ae">UIImage</span> <span style="font-variant-ligatures: no-common-ligatures; color: #3d1d81">imageNamed</span>:<span style="font-variant-ligatures: no-common-ligatures; color: #c91b13">@"lufy"</span>].<span style="font-variant-ligatures: no-common-ligatures; color: #703daa">CGImage</span>;</p>
图层的transform属性
//layer 的3D缩放比例 layer.transform = CATransform3DMakeScale(1.5, 0.5, 0); //图层的旋转 layer.transform = CATransform3DMakeRotation(M_PI_4, 0, 0, 1); //下面是用kvc 的方法设置图层的变换属性 NSValue *value = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_4, 0, 0, 1)]; [layer setValue:value forKeyPath:@"transform"]; [layer setValue:@(M_PI_2) forKeyPath:@"transform.rotation"]; layer.transform = CATransform3DMakeScale(0.5, 2, 0); [layer setValue:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.5, 2, 0)] forKeyPath:@"transform"]; // 可以传递哪些key path, 在官方文档搜索 "CATransform3D key paths" [layer setValue:@(-100) forKeyPath:@"transform.translation.x"];position 就是图层在父视图中的位置 默认为CGPointZero
layer.position =
CGPointZero;
anchorPoint是layer的抛锚点,坐标系是自己的坐标系 是把自己的那个点放在设置的position 上 默认为CGPointZero
并且如果设置,x和y的值都是0——1 之间 。
layer.anchorPoint =
CGPointZero;
layer的隐式动画
layer.backgroundColor = [UIColor blueColor].CGColor; [CATransaction begin]; // 开启事务 //关闭隐式动画 默认是不关闭 也就是NO [CATransaction setDisableActions:NO]; self.layer.position = CGPointMake(100, 100); self.layer.opacity = 0.5; [CATransaction commit]; // 提交事务
自定义layer
自定义layer是为了实现自己想要的一些效果,比如在图层上画圆,画线等一些我们自己特别定制的效果
这个时候我们需要创建一个类继承与CALayer 但是我们需要在ViewController中从新渲染
ViewController中的代码
MyLayer *layer = [MyLayer layer]; layer.bounds = CGRectMake(0, 0, 100, 100); layer.backgroundColor = [UIColor blueColor].CGColor; layer.anchorPoint = CGPointZero; [layer setNeedsDisplay]; [self.view.layer addSublayer:layer];
自定义的layer类中需要从写方法
- (void)drawInContext:(CGContextRef)ctx
/** * 只有明显地调用setNeedsDisplay方法,才会调用drawInContext:方法进行绘制 */ - (void)drawInContext:(CGContextRef)ctx { // 红色 CGContextSetRGBFillColor(ctx, 1, 0, 0, 1); // 添加圆 CGContextAddEllipseInRect(ctx, CGRectMake(0, 0, 50, 50)); // 实心绘制 CGContextFillPath(ctx); }
另外我们还可以设置代理,在这里我有必要说明一下图层设置代理
图层设置代理的时候不必遵守任何协议 图层的代理应该设置为自己的View,自己的View的图层又是自己 例如UIImageView的对象imgV的layer的代理应该设置为imgV,不能设置为其他
#pragma mark - 图层的代理方法 - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx { CGContextSetRGBFillColor(ctx, 1, 0, 0, 1); CGContextAddRect(ctx, CGRectMake(0, 0, 20, 20)); CGContextFillPath(ctx); }
希望这篇文章能够对你起到帮助
相关文章推荐
- #在蓝懿学习iOS的日子#第四个练习日
- iOS 调用支付宝 显示系统繁忙,请稍后再试(ALI10
- 如何自定义NavigationBar的高度
- iOS 开发懒加载
- iOS开发>学无止境 - 验证码倒计时
- 我的iOS学习历程 - OC第七天
- 我的iOS学习历程 - OC第七天
- iOS模糊效果使用第三方库FXBlurView
- 总结分享10大iOS开发者最喜爱的库
- 汇总iOS开发中需要用到的开源库
- IOS开发: 返回字符串高度, 重定义cell高度, cell重用方法
- iOS9 3DTouch、ShortcutItem、Peek And Pop
- iOS(8)iOS中几种数据持久化方案:我要永远地记住你!
- SDWebImage的使用
- IOS 下载、查询等
- iOS开发 实现QQ第三方登录
- iOS 得到当前时间之前和之后N天的日期
- 开源 iOS 项目分类索引大全
- iOS开发中如何隐藏导航栏下的线条
- iOS提交被拒