首页 > 代码库 > CALayer

CALayer

[objc] view plaincopy在CODE上查看代码片派生到我的代码片    <span style="font-size:18px;">为了确认设置</span><span style="font-size:18px; font-family: Arial, Helvetica, sans-serif;">imageView.layer.masksToBounds = YES</span><span style="font-family: Arial, Helvetica, sans-serif;">后,</span><span style="font-size:18px;"></span><p class="p1"><span style="font-family: Arial, Helvetica, sans-serif;">外面的view是否消失,改用了UIButton来验证.结果是存在的.</span></p>  [objc] view plaincopy在CODE上查看代码片派生到我的代码片    UIButton *imageView = [[UIButton alloc] initWithFrame:CGRectMake(30, 20, 200, 200)];      [imageView setBackgroundImage:[UIImage imageNamed:@"气球.png"] forState:UIControlStateNormal];      [self.view addSubview:imageView];            /*****--第一种:图层当作一个属性来使用 --------*/      //CALayer图层前需要导入一个系统库      //设置图层的宽度      [imageView.layer setBorderWidth:5];      //设置图层的弧度      [imageView.layer setCornerRadius:100];      //设置颜色      [imageView.layer setBorderColor:[UIColor purpleColor].CGColor];      //设置bounds外消失      //imageView.layer.masksToBounds = YES;//默认为NO,如果设置为YES时,连阴影也会没有      //设置透明度      imageView.layer.opacity = 0.9;            //设置阴影      imageView.layer.shadowColor = [UIColor redColor].CGColor;      //设置阴影偏移量      imageView.layer.shadowOffset = CGSizeMake(30, 30);      //设置阴影透明度,默认是全透明,所以设置才能看到      imageView.layer.shadowOpacity = 0.9f;      //设置阴影弧度      imageView.layer.shadowRadius = 50;             /*****--第二种:图层单独使用 --------*/      CALayer *layer1 = [CALayer layer];//便利构造器创建一个图层      //设置一个图层的内容      layer1.contents = (id)[UIImage imageNamed:@"气球.png"].CGImage;      //设置图层的fram      layer1.frame = CGRectMake(30, 260, 200, 200);      //把图层add到图层上面      [self.view.layer addSublayer:layer1];      [layer1 setCornerRadius :100];  上面有一个问题就是:我设置imageView.layer.masksToBounds = YES后,连阴影部分都消失了.为了解决这个问题,我们可以采用两层图层;代码如下:[objc] view plaincopy在CODE上查看代码片派生到我的代码片    CALayer *sublayer =[CALayer layer];      sublayer.backgroundColor =[UIColor blueColor].CGColor;      sublayer.shadowOffset = CGSizeMake(3, 10);      sublayer.shadowRadius =5.0;      sublayer.shadowColor =[UIColor blackColor].CGColor;      sublayer.shadowOpacity =0.8;      sublayer.frame = CGRectMake(30, 30, 128, 192);      sublayer.borderColor =[UIColor blackColor].CGColor;      sublayer.borderWidth =2.0;      sublayer.cornerRadius =10.0;      [self.view.layer addSublayer:sublayer];            CALayer *imageLayer =[CALayer layer];      imageLayer.frame = sublayer.bounds;      imageLayer.cornerRadius =10.0;      imageLayer.contents =(id)[UIImage imageNamed:@"气球.png"].CGImage;      imageLayer.masksToBounds =YES;      [sublayer addSublayer:imageLayer];

 

CALayer