指定UIView的某几个角为圆角
如果需要将UIView的四个角全部设置为圆角,做法相当简单,只需要设置其layer的cornerRadius属性即可。而若要指定某几个角(小于4)为圆角,而别的角不变的时候,这种方法就不好用了。这种情况,最简单优雅的方案,就是使用UIBezierPath。示例代码如下:
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(120, 10, 80, 80)]; view.backgroundColor = [UIColor grayColor]; [self.view addSubview:view]; UIBezierPath * maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)]; CAShapeLayer * maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = view.bounds; maskLayer.path = maskPath.CGPath; view.layer.mask = maskLayer;
[maskLayer release];
代码中,byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight指定了成为圆角的角。该参数是UIRectCorner类型,可选值有:
* UIRectCornerTopLeft
* UIRectCornerTopRight
* UIRectCornerBottomLeft
* UIRectCornerBottomRight
* UIRectCornerAllCorners
注意,项目中需要加入QuartzCore.framework框架,导入<QuartzCore/QuartzCore.h>头文件