iOS 随意设置View的角为圆角

整理下之前的tips:
大家都知道怎么设置view的四个角为圆角,但是在iOS几乎很少遇到随意设置View的角为圆角,如果遇到了,希望大家知道怎么做,有一个大体的思路。

UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(120, 10, 80, 80)];
view2.backgroundColor = [UIColor redColor];
[self.view addSubview:view2];
    
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view2.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = view2.bounds;
maskLayer.path = maskPath.CGPath;
view2.layer.mask = maskLayer;```
其中UIRectCorner参数:
  • UIRectCornerTopLeft
  • UIRectCornerTopRight
  • UIRectCornerBottomLeft
  • UIRectCornerBottomRight
  • UIRectCornerAllCorners

你可能感兴趣的:(iOS 随意设置View的角为圆角)