我通常使用cornerRadius
属性将圆角应用于视图图层。当视图只需要圆角顶角或底角时,我会应用带有贝塞尔曲线路径的蒙版(使用bezierPathWithRoundedRect:byRoundingCorners:cornerRadii:
)。当两种方法在同一视图层次结构中组合时,圆角未正确对齐,如下所示:
可以使用以下代码复制此简化示例:
@interface ViewController : UIViewController
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
CGRect frame = CGRectMake(50.0, 50.0, 100.0, 100.0);
CGFloat radius = 20.0;
// Apply cornerRadius to green backdrop view.
UIView *backdropView = [[UIView alloc] initWithFrame:frame];
backdropView.backgroundColor = [UIColor greenColor];
backdropView.layer.cornerRadius = radius;
[self.view addSubview:backdropView];
// Apply bezier path mask to black front view.
UIView *frontView = [[UIView alloc] initWithFrame:frame];
frontView.backgroundColor = [UIColor blackColor];
CAShapeLayer * maskLayer = [CAShapeLayer layer];
maskLayer.path = [UIBezierPath bezierPathWithRoundedRect:frontView.bounds
byRoundingCorners:UIRectCornerAllCorners
cornerRadii:CGSizeMake(radius, radius)].CGPath;
frontView.layer.mask = maskLayer;
[self.view addSubview:frontView];
}
@end
设置所涉及的不同层的shouldRasterize
属性并未解决问题。我想明白为什么会这样。一种可能的解决方法是始终应用贝塞尔路径掩模,而不是简单地设置角半径,但感觉就像过度杀伤。
答案 0 :(得分:2)
这个网站做得很好解释:http://www.paintcodeapp.com/blogpost/code-for-ios-7-rounded-rectangles(简而言之,它只是iOS7的一部分)
有关额外说明,请参阅: http://www.mani.de/backstage/?p=483