iOS开发设置view某几个角为圆角

我们知道设置四个角都为圆角很简单,创建一个view,设置其layer.cornerRadius即可,代码如下:
    UIView *testview = [[UIView alloc] init];
    testview.layer.cornerRadius = 10;
    [self.view addSubview: testview];
其实指定圆角也是通过view的layer属性来设置的,我通过设置控件的上面两个角为圆角来举例,代码如下:
 
 UIView *testview = [[UIView alloc] init];
    [self.view addSubview: testview];

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect: testview.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(10,10)];
    //创建 layer
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = testview.bounds;
    //赋值
    maskLayer.path = maskPath.CGPath;
    testview.layer.mask = maskLayer;
首先创建view,然后单独设置其layer的方法,再将其赋值给view的layer属性即可,通过方法里面的参数UIRectCornerTopLeft,UIRectCornerTopRight我们便可以看出这是设置其左上角以及右上角为圆角,在cornerRadii:中设置圆角尺寸即可实现我们想要的效果。
原文地址:https://www.cnblogs.com/-yun/p/12133495.html