- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//view的layer层的设置和ImageView的layer层的设置
//注意了:在滚动视图上一般不要设置layer的边框,阴影,因为比较耗费内存
//一.创建view
UIView *view
= [[UIView alloc] initWithFrame:CGRectMake(20,
20, 100, 100)];
view.backgroundColor =
[UIColor cyanColor];
[self.view addSubview:view];
//1.在UIView上修改layer层的背景颜色(图层与UIView是一样大的)
view.layer.backgroundColor =
[UIColor yellowColor].CGColor;
//2.layer层修改透明度:opacity,0.0-1.0;(0.0时UIView将看不见)
view.layer.opacity =
0.8;
//3.设置圆角的,设置的值为10.0
view.layer.cornerRadius =
20.0;
//4.设置边框
view.layer.borderWidth =
10.0;
//5.设置边框颜色
view.layer.borderColor =
[UIColor purpleColor].CGColor;
//6.阴影部分的设置 (注意阴影部分的透明度默认为0)
view.layer.shadowOpacity =
0.8;
view.layer.shadowColor =
[UIColor brownColor].CGColor;
//阴影部分的偏移量:(若都为正,则往右下偏移)
view.layer.shadowOffset = CGSizeMake(10,
10);
//7.anchorPoint锚点:绕着某个点进行旋转(轻易不要动锚点)
NSLog(@"frame
= %@", NSStringFromCGRect(view.layer.frame)); //得到的结果:
frame = {{20, 20}, {100, 100}}
NSLog(@"anchorPoint
= %@", NSStringFromCGPoint(view.layer.anchorPoint)); //anchorPoint
= {0.5, 0.5}
//position是某点基于父视图原点的坐标,(相当于view的center)
NSLog(@"position
= %@", NSStringFromCGPoint(view.layer.position)); //position
= {70, 70}
//8.在图层上添加一个子图层
//1>先创建一个图层
CALayer *subLayer
= [CALayer layer];
//2>设置它的frame,backgroundColor
subLayer.frame = CGRectMake(10,
10, 50, 50);
subLayer.backgroundColor =
[UIColor grayColor].CGColor;
//3>在view的layer上加上一个子图层
[view.layer addSublayer:subLayer];
//二.创建UIImageView
UIImage *image
= [UIImage imageNamed:@"a1.jpg"];
UIImageView *imageView
= [[UIImageView alloc] initWithFrame:CGRectMake(180,
20, 100, 100)];
imageView.image =
image;
[self.view addSubview:imageView];
//1>创建imageView图层的阴影部分
imageView.layer.shadowRadius =
10.0;
imageView.layer.shadowOpacity =
1.0;
imageView.layer.shadowOffset =CGSizeMake(10,
-10);
//2>注意imageView:设置圆角时必须设置将masksToBounds打开,但是masksToBounds打开时,不会显示阴影
//解决方案一:设置两个layer,一个layer用于控制阴影,一个layer用于圆角
imageView.layer.cornerRadius =
20;
imageView.layer.masksToBounds = YES;
}
{
// Do any additional setup after loading the view.
}