UIView设置少于四个的圆角

序言

最近的需求中有个UIView需要设置左上角和右上角为圆角,其余两个为直角,一开始用的是重写drawRect,然后用绘图重绘每个角的样子,计算起来还是麻烦.

后来发现了下面的方法:

- (void)drawLeftTopCircleCorner {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    view.center = CGPointMake(self.view.bounds.size.width * 0.25, self.view.bounds.size.height * 0.25);
    view.backgroundColor = [UIColor redColor];
    view.layer.masksToBounds = YES;
    
    //这里设置需要绘制的圆角
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds
                                                   byRoundingCorners:UIRectCornerTopLeft//左上角
                                                         cornerRadii:CGSizeMake(25, 25)];//设置圆角大小,弧度为3
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = view.bounds;
    maskLayer.path = maskPath.CGPath;
    view.layer.mask = maskLayer;
    [self.view addSubview:view];
}

效果图


UIView设置少于四个的圆角_第1张图片
circleCorner.png
通过这几个枚举值判断画哪个圆角
typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
    UIRectCornerTopLeft     = 1 << 0,
    UIRectCornerTopRight    = 1 << 1,
    UIRectCornerBottomLeft  = 1 << 2,
    UIRectCornerBottomRight = 1 << 3,
    UIRectCornerAllCorners  = ~0UL
};

项目连接地址
本文参考iOS UIView设置少于四个的圆角,感谢该作者

你可能感兴趣的:(UIView设置少于四个的圆角)