带圆角控件设置阴影

分析原因:因为代码中设置了masksToBounds属性为YES了,将后面设置的阴影效果给裁剪掉了,所以我们看不到阴影效果,如果我们将masksToBounds属性为NO了,这样就会失去圆角效果

解决方案:给imageView添加一个父视图,在父视图上添加阴影效果就好,这样就不会对imageView的圆角造成影响了

实例代码:UIImageView*imgView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0,150,200)];

    imgView.layer.masksToBounds = YES;

    imgView.layer.cornerRadius=20;

    imgView.backgroundColor = [UIColor whiteColor];

    UIView*shadowView = [[UIViewalloc]initWithFrame:CGRectMake(200,200,150,200)];

    [self.viewaddSubview:shadowView];

    shadowView.layer.shadowColor = [UIColor blackColor].CGColor;

    shadowView.layer.shadowOffset = CGSizeMake(0, 2);

    shadowView.layer.shadowOpacity=0.2;

    shadowView.layer.shadowRadius=3.0;

    shadowView.layer.cornerRadius=3.0;

    shadowView.clipsToBounds=NO;

    [shadowViewaddSubview:imgView];

效果截图:


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容