我們知道設置四個角都為圓角很簡單,創建一個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:中設置圓角尺寸即可實現我們想要的效果。