在UIView中的两个角

不久前,我发布了一个关于仅将视图的两个角取整的问题,并获得了不错的答复,但是在实现它时遇到了问题。这是我的drawRect:方法:


- (void)drawRect:(CGRect)rect {

    //[super drawRect:rect]; <------Should I uncomment this?

    int radius = 5;

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextBeginPath(context);

    CGContextAddArc(context, rect.origin.x + radius, rect.origin.y + rect.size.height - radius, radius, M_PI, M_PI / 2, 1);

    CGContextAddArc(context, rect.origin.x + rect.size.width - radius, rect.origin.y + rect.size.height - radius, radius, M_PI / 2, 0.0f, 1);

    CGContextClosePath(context);

    CGContextClip(context);

}

该方法正在被调用,但似乎并不影响视图的结果。有什么想法吗?


富国沪深
浏览 565回答 3
3回答

当年话下

iOS 11中引入的CACornerMask可帮助在视图层中定义左上,右上,左下,右下。以下是使用示例。在这里,我尝试仅舍入两个上角:myView.clipsToBounds = truemyView.layer.cornerRadius = 10myView.layer.maskedCorners = [.layerMinXMinYCorner,.layerMaxXMinYCorner]提前致谢。仅供参考:&nbsp;

POPMUISE

据我所知,如果还需要遮罩子视图,则可以使用CALayer遮罩。有两种方法可以做到这一点。第一个比较优雅,第二个是一种解决方法:-),但它也很快。两者都是基于CALayer遮罩的。去年,我在几个项目中都使用了这两种方法,然后希望您能找到有用的东西。解决方案1首先,我创建了此函数以动态生成UIImage带有所需圆角的图像蒙版()。此函数本质上需要5个参数:图像的边界和4个角半径(左上,右上,左下和右下)。static inline UIImage* MTDContextCreateRoundedMask( CGRect rect, CGFloat radius_tl, CGFloat radius_tr, CGFloat radius_bl, CGFloat radius_br ) {&nbsp;&nbsp;&nbsp; &nbsp; CGContextRef context;&nbsp; &nbsp; CGColorSpaceRef colorSpace;&nbsp; &nbsp; colorSpace = CGColorSpaceCreateDeviceRGB();&nbsp; &nbsp; // create a bitmap graphics context the size of the image&nbsp; &nbsp; context = CGBitmapContextCreate( NULL, rect.size.width, rect.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast );&nbsp; &nbsp; // free the rgb colorspace&nbsp; &nbsp; CGColorSpaceRelease(colorSpace);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if ( context == NULL ) {&nbsp; &nbsp; &nbsp; &nbsp; return NULL;&nbsp; &nbsp; }&nbsp; &nbsp; // cerate mask&nbsp; &nbsp; CGFloat minx = CGRectGetMinX( rect ), midx = CGRectGetMidX( rect ), maxx = CGRectGetMaxX( rect );&nbsp; &nbsp; CGFloat miny = CGRectGetMinY( rect ), midy = CGRectGetMidY( rect ), maxy = CGRectGetMaxY( rect );&nbsp; &nbsp; CGContextBeginPath( context );&nbsp; &nbsp; CGContextSetGrayFillColor( context, 1.0, 0.0 );&nbsp; &nbsp; CGContextAddRect( context, rect );&nbsp; &nbsp; CGContextClosePath( context );&nbsp; &nbsp; CGContextDrawPath( context, kCGPathFill );&nbsp; &nbsp; CGContextSetGrayFillColor( context, 1.0, 1.0 );&nbsp; &nbsp; CGContextBeginPath( context );&nbsp; &nbsp; CGContextMoveToPoint( context, minx, midy );&nbsp; &nbsp; CGContextAddArcToPoint( context, minx, miny, midx, miny, radius_bl );&nbsp; &nbsp; CGContextAddArcToPoint( context, maxx, miny, maxx, midy, radius_br );&nbsp; &nbsp; CGContextAddArcToPoint( context, maxx, maxy, midx, maxy, radius_tr );&nbsp; &nbsp; CGContextAddArcToPoint( context, minx, maxy, minx, midy, radius_tl );&nbsp; &nbsp; CGContextClosePath( context );&nbsp; &nbsp; CGContextDrawPath( context, kCGPathFill );&nbsp; &nbsp; // Create CGImageRef of the main view bitmap content, and then&nbsp; &nbsp; // release that bitmap context&nbsp; &nbsp; CGImageRef bitmapContext = CGBitmapContextCreateImage( context );&nbsp; &nbsp; CGContextRelease( context );&nbsp; &nbsp; // convert the finished resized image to a UIImage&nbsp;&nbsp; &nbsp; UIImage *theImage = [UIImage imageWithCGImage:bitmapContext];&nbsp; &nbsp; // image is retained by the property setting above, so we can&nbsp;&nbsp; &nbsp; // release the original&nbsp; &nbsp; CGImageRelease(bitmapContext);&nbsp; &nbsp; // return the image&nbsp; &nbsp; return theImage;}&nbsp;&nbsp;现在,您只需要几行代码。我把东西在我的viewController viewDidLoad的方法,因为它的速度更快,但你可以在自定义也用它UIView与layoutSubviews实例方法。- (void)viewDidLoad {&nbsp; &nbsp; // Create the mask image you need calling the previous function&nbsp; &nbsp; UIImage *mask = MTDContextCreateRoundedMask( self.view.bounds, 50.0, 50.0, 0.0, 0.0 );&nbsp; &nbsp; // Create a new layer that will work as a mask&nbsp; &nbsp; CALayer *layerMask = [CALayer layer];&nbsp; &nbsp; layerMask.frame = self.view.bounds;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; // Put the mask image as content of the layer&nbsp; &nbsp; layerMask.contents = (id)mask.CGImage;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; // set the mask layer as mask of the view layer&nbsp; &nbsp; self.view.layer.mask = layerMask;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // Add a backaground color just to check if it works&nbsp; &nbsp; self.view.backgroundColor = [UIColor redColor];&nbsp; &nbsp; // Add a test view to verify the correct mask clipping&nbsp; &nbsp; UIView *testView = [[UIView alloc] initWithFrame:CGRectMake( 0.0, 0.0, 50.0, 50.0 )];&nbsp; &nbsp; testView.backgroundColor = [UIColor blueColor];&nbsp; &nbsp; [self.view addSubview:testView];&nbsp; &nbsp; [testView release];&nbsp; &nbsp; [super viewDidLoad];}解决方案2这个解决方案有点“肮脏”。本质上,您可以使用所需的圆角(所有角)创建遮罩层。然后,应通过角半径的值增加蒙版层的高度。这样,底部的圆角被隐藏了,您只能看到上面的圆角。我把代码只是在viewDidLoad方法,因为它的速度更快,但你可以在自定义也用它UIView与layoutSubviews实例方法。&nbsp;&nbsp;- (void)viewDidLoad {&nbsp; &nbsp; // set the radius&nbsp; &nbsp; CGFloat radius = 50.0;&nbsp; &nbsp; // set the mask frame, and increase the height by the&nbsp;&nbsp; &nbsp; // corner radius to hide bottom corners&nbsp; &nbsp; CGRect maskFrame = self.view.bounds;&nbsp; &nbsp; maskFrame.size.height += radius;&nbsp; &nbsp; // create the mask layer&nbsp; &nbsp; CALayer *maskLayer = [CALayer layer];&nbsp; &nbsp; maskLayer.cornerRadius = radius;&nbsp; &nbsp; maskLayer.backgroundColor = [UIColor blackColor].CGColor;&nbsp; &nbsp; maskLayer.frame = maskFrame;&nbsp; &nbsp; // set the mask&nbsp; &nbsp; self.view.layer.mask = maskLayer;&nbsp; &nbsp; // Add a backaground color just to check if it works&nbsp; &nbsp; self.view.backgroundColor = [UIColor redColor];&nbsp; &nbsp; // Add a test view to verify the correct mask clipping&nbsp; &nbsp; UIView *testView = [[UIView alloc] initWithFrame:CGRectMake( 0.0, 0.0, 50.0, 50.0 )];&nbsp; &nbsp; testView.backgroundColor = [UIColor blueColor];&nbsp; &nbsp; [self.view addSubview:testView];&nbsp; &nbsp; [testView release];&nbsp; &nbsp; [super viewDidLoad];}希望这可以帮助。再见!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

iOS