使UIView的特定角落成圆形

use*_*992 7 iphone uiview ios

我做了一些研究,我看,有没有简单的方法,使只有右上和UIView的圆左上死角,对不对?

PS.这段代码使得所有4个角都是圆形的.我不想要.

  #import <QuartzCore/QuartzCore.h>

 self.layer.cornerRadius = 5;
 self.layer.masksToBounds = YES;
Run Code Online (Sandbox Code Playgroud)

amb*_*amb 21

您可以使用此代码段执行此操作:

// Set top right & left corners
[self setMaskTo:yourView byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight];
Run Code Online (Sandbox Code Playgroud)

编辑

对不起,我忘了这个

- (void)setMaskTo:(UIView*)view byRoundingCorners:(UIRectCorner)corners
{
    UIBezierPath *rounded = [UIBezierPath bezierPathWithRoundedRect:view.bounds
                                                  byRoundingCorners:corners
                                                        cornerRadii:CGSizeMake(8.0, 8.0)];
    CAShapeLayer *shape = [[CAShapeLayer alloc] init];
    [shape setPath:rounded.CGPath];    
    view.layer.mask = shape;
}
Run Code Online (Sandbox Code Playgroud)

PS:正如我在评论中所说,如果你在显示UITableViewCell单元格时使用这样的东西,我发现使用背景图像总是更好的选择,因为这种绘图材料总会影响性能.

  • 这个评论应该在答案中说明,掩盖一个视图是一个很大的性能影响,如果你可以帮助它,应该始终使用背景图像 (2认同)