iOS给view设置部分圆角
2016-06-08 本文已影响4509人
不简单的风度
直接上代码
UIImage *img = [UIImage ly_imageWithColor:[UIColor redColor]];
self.imgView = [[UIImageView alloc] initWithImage:img];
self.imgView.frame = CGRectMake(100, 100, 100, 100);
[self.view addSubview:self.imgView];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.imgView.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.imgView.bounds;
maskLayer.path = maskPath.CGPath;
self.imgView.layer.mask = maskLayer;
效果如图:
Paste_Image.png关于参数:
第二个参数byRoundingCorners:(UIRectCorner)corners
允许指定矩形的部分角为圆角,而其余的角为直角,取值来自枚举:
typedef NS_OPTIONS(NSUInteger, UIRectCorner) {
UIRectCornerTopLeft = 1 << 0,
UIRectCornerTopRight = 1 << 1,
UIRectCornerBottomLeft = 1 << 2,
UIRectCornerBottomRight = 1 << 3,
UIRectCornerAllCorners = ~0UL
};
其最后一个参数cornerRadii:(CGSize)cornerRadii
指定了圆角的半径,但这里需要注意,这个参数的取值是 CGSize类型,也就意味着这里需要给出的是椭圆的半径。