.h文件
</pre><pre code_snippet_id="357860" snippet_file_name="blog_20140522_1_4615163" name="code" class="objc">@interface UIView (RectCorner) - (void)setCornerOnTop; - (void)setCornerOnBottom; - (void)setAllCorner; - (void)setNoneCorner; @end
.m 文件
</pre><pre>
@implementation UIView (RectCorner)
- (void)setCornerOnTop
{
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)
cornerRadii:CGSizeMake(10.0f, 10.0f)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
self.layer.mask = maskLayer;
[maskLayer release];
}
- (void)setCornerOnBottom
{
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)
cornerRadii:CGSizeMake(10.0f, 10.0f)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
self.layer.mask = maskLayer;
[maskLayer release];
}
- (void)setAllCorner
{
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
cornerRadius:10.0];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
self.layer.mask = maskLayer;
[maskLayer release];
}
- (void)setNoneCorner
{
self.layer.mask = nil;
}
@end
转载自:http://blog.csdn.net/reylen/article/details/22038607