指定UIView的某几个角为圆角

2,879 阅读1分钟

在做iOS UI开发的时候我们会经常遇到要把一个矩形view的直角切成圆角,切圆角分两种情况:

1、把view的四个直角都切成圆角:

    //设置圆角半径值
    self.view.layer.cornerRadius  = 10.f;
    //设置为遮罩,除非view有阴影,否则都要指定为YES的
    self.view.layer.masksToBounds = YES;

2、指定角设置圆角

    //把 view2 的 左下角 和 右下角的直角切成圆角
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(120,10,80,80)];
    view2.backgroundColor = [UIColor redColor];
    [self.view addSubview:view2];
    
    //设置切哪个直角
//    UIRectCornerTopLeft     = 1 << 0,  左上角
//    UIRectCornerTopRight    = 1 << 1,  右上角
//    UIRectCornerBottomLeft  = 1 << 2,  左下角
//    UIRectCornerBottomRight = 1 << 3,  右下角
//    UIRectCornerAllCorners  = ~0UL     全部角
    //得到view的遮罩路径
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view2.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10,10)];
    //创建 layer
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = view2.bounds;
    //赋值
    maskLayer.path = maskPath.CGPath;
    view2.layer.mask = maskLayer;

UIBezierPath是什么?