做iOS開發(fā)的都會知道,給一個視圖設置圓角只需要設置layer.cornerRadius塔沃,并masksToBounds值為YES即可,但若還想設置陰影效果,發(fā)現圓角和陰影是不兼容的双絮,這里提供兩種方法同時設置圓角和陰影
①父視圖陰影,子視圖圓角
UIView *shadowView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
[self.view addSubview:shadowView];
shadowView.layer.shadowColor = [UIColor blackColor].CGColor;
shadowView.layer.shadowOffset = CGSizeMake(0, 0);
shadowView.layer.shadowOpacity = 0.8;
shadowView.layer.shadowRadius = 9.0;
shadowView.layer.cornerRadius = 9.0;
UILabel *label = [[UILabel alloc] initWithFrame:shadowView.bounds];
label.backgroundColor =[UIColor redColor];
label.layer.cornerRadius = 10;
label.layer.masksToBounds = YES;
[shadowView addSubview:label];
②高效的使用shadowPath
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(100, 400, 200, 200)];
view.backgroundColor = [UIColor redColor];
view.layer.shadowColor = [UIColor blackColor].CGColor;//陰影顏色
view.layer.shadowOpacity = 0.8;//陰影透明度得问,默認為0囤攀,如果不設置的話看不到陰影,切記宫纬,這是個大坑
view.layer.shadowOffset = CGSizeMake(0, 0);//設置偏移量
view.layer.cornerRadius = 9.0;
view.layer.shadowRadius = 9.0;
[self.view addSubview:view];
//參數依次為大小焚挠,設置四個角圓角狀態(tài),圓角曲度
view.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:5 cornerRadii:CGSizeMake(0, 0)].CGPath;
效果圖如下