带圆角控件设置阴影

分析原因:因为代码中设置了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];

效果截图:


你可能感兴趣的:(带圆角控件设置阴影)