CALayer 图层
2015-11-17 20:30 xiangjune 阅读(209) 评论(0) 编辑 收藏 举报
// CALayer 图层属性,继承UIView都有该属性,可设置边框宽度、颜色、圆角、阴影等
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 200, 150)];
imageView.image = [UIImage imageNamed:@"test.jpg"];
// 边框宽度、颜色
imageView.layer.borderWidth = 5;
imageView.layer.borderColor = [UIColor redColor].CGColor;
// 边框圆角
imageView.layer.cornerRadius = 20;
// 边框圆角超出部分隐藏 设置该属性后,阴影设置无效
// imageView.layer.masksToBounds = YES;
// 阴影
imageView.layer.shadowColor = [UIColor blackColor].CGColor;
imageView.layer.shadowOffset = CGSizeMake(5, 5);
imageView.layer.shadowOpacity = 0.5;
[self.view addSubview:imageView];
// CALayer 创建、显示
// 比较UIView,CALayer不能处理事件,更加轻量级
CALayer *newLayer = [CALayer layer]; // 创建
newLayer.bounds = CGRectMake(100, 330, 200, 150); // 位置 必须设置
newLayer.position = CGPointMake(100, 330); // 位置 必须设置
// anchorPoint 定义的是CALayer中的点定在position位置 0-1,默认(0.5,0.5)是中心点 (0,0)是左上顶点 (1,1)右下点
newLayer.anchorPoint = CGPointMake(0, 0.5);
newLayer.contents = (id)[UIImage imageNamed:@"test.jpg"].CGImage;
newLayer.borderColor = [UIColor blackColor].CGColor;
newLayer.borderWidth = 5;
newLayer.cornerRadius = 10;
newLayer.masksToBounds = YES;
[self.view.layer addSublayer:newLayer];