据我所知,如果还需要遮罩子视图,则可以使用CALayer
遮罩。有两种方法可以做到这一点。第一个比较优雅,第二个是一种解决方法:-),但是它也很快。两者都是基于CALayer
遮罩的。去年,我在几个项目中都使用了这两种方法,然后希望您能找到有用的东西。
解决方案1
首先,我创建了此函数以动态生成UIImage
带有所需圆角的图像蒙版()。此函数本质上需要5个参数:图像的边界和4个角半径(左上,右上,左下和右下)。
static inline UIImage* MTDContextCreateRoundedMask( CGRect rect, CGFloat radius_tl, CGFloat radius_tr, CGFloat radius_bl, CGFloat radius_br ) {
CGContextRef context;
CGColorSpaceRef colorSpace;
colorSpace = CGColorSpaceCreateDeviceRGB();
context = CGBitmapContextCreate( NULL, rect.size.width, rect.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast );
CGColorSpaceRelease(colorSpace);
if ( context == NULL ) {
return NULL;
}
CGFloat minx = CGRectGetMinX( rect ), midx = CGRectGetMidX( rect ), maxx = CGRectGetMaxX( rect );
CGFloat miny = CGRectGetMinY( rect ), midy = CGRectGetMidY( rect ), maxy = CGRectGetMaxY( rect );
CGContextBeginPath( context );
CGContextSetGrayFillColor( context, 1.0, 0.0 );
CGContextAddRect( context, rect );
CGContextClosePath( context );
CGContextDrawPath( context, kCGPathFill );
CGContextSetGrayFillColor( context, 1.0, 1.0 );
CGContextBeginPath( context );
CGContextMoveToPoint( context, minx, midy );
CGContextAddArcToPoint( context, minx, miny, midx, miny, radius_bl );
CGContextAddArcToPoint( context, maxx, miny, maxx, midy, radius_br );
CGContextAddArcToPoint( context, maxx, maxy, midx, maxy, radius_tr );
CGContextAddArcToPoint( context, minx, maxy, minx, midy, radius_tl );
CGContextClosePath( context );
CGContextDrawPath( context, kCGPathFill );
CGImageRef bitmapContext = CGBitmapContextCreateImage( context );
CGContextRelease( context );
UIImage *theImage = [UIImage imageWithCGImage:bitmapContext];
CGImageRelease(bitmapContext);
return theImage;
}
现在,您只需要几行代码。我把东西在我的viewControllerviewDidLoad
的方法,因为它的速度更快,但你可以在自定义也用它UIView
与layoutSubviews
实例方法。
- (void)viewDidLoad {
UIImage *mask = MTDContextCreateRoundedMask( self.view.bounds, 50.0, 50.0, 0.0, 0.0 );
CALayer *layerMask = [CALayer layer];
layerMask.frame = self.view.bounds;
layerMask.contents = (id)mask.CGImage;
self.view.layer.mask = layerMask;
self.view.backgroundColor = [UIColor redColor];
UIView *testView = [[UIView alloc] initWithFrame:CGRectMake( 0.0, 0.0, 50.0, 50.0 )];
testView.backgroundColor = [UIColor blueColor];
[self.view addSubview:testView];
[testView release];
[super viewDidLoad];
}
解决方案2
这个解决方案有点“肮脏”。本质上,您可以使用所需的圆角(所有角)创建遮罩层。然后,应通过角半径的值来增加遮罩层的高度。这样,底部的圆角被隐藏了,您只能看到上面的圆角。我把代码放在viewDidLoad
方法,因为它的速度更快,但你可以在自定义也用它UIView
与layoutSubviews
实例方法。
- (void)viewDidLoad {
CGFloat radius = 50.0;
CGRect maskFrame = self.view.bounds;
maskFrame.size.height += radius;
CALayer *maskLayer = [CALayer layer];
maskLayer.cornerRadius = radius;
maskLayer.backgroundColor = [UIColor blackColor].CGColor;
maskLayer.frame = maskFrame;
self.view.layer.mask = maskLayer;
self.view.backgroundColor = [UIColor redColor];
UIView *testView = [[UIView alloc] initWithFrame:CGRectMake( 0.0, 0.0, 50.0, 50.0 )];
testView.backgroundColor = [UIColor blueColor];
[self.view addSubview:testView];
[testView release];
[super viewDidLoad];
}
希望这可以帮助。再见!