分析原因:因?yàn)榇a中設(shè)置了masksToBounds屬性為YES了,將后面設(shè)置的陰影效果給裁剪掉了,所以我們看不到陰影效果,如果我們將masksToBounds屬性為NO了,這樣就會失去圓角效果
解決方案:給imageView添加一個父視圖,在父視圖上添加陰影效果就好,這樣就不會對imageView的圓角造成影響了
實(shí)例代碼: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];
效果截圖:
