Ios 为什么应用bezierPathWithRoundedRect蒙版会产生与设置图层不同的结果';转弯半径?
我通常使用Ios 为什么应用bezierPathWithRoundedRect蒙版会产生与设置图层不同的结果';转弯半径?,ios,objective-c,calayer,uibezierpath,cashapelayer,Ios,Objective C,Calayer,Uibezierpath,Cashapelayer,我通常使用cornerRadius属性将圆角应用于视图图层。当视图只需要圆角的顶角或底角时,我会应用带有贝塞尔路径的遮罩(使用贝塞尔路径WithRoundedRect:byRoundingCorners:cornerRadii:)。当两种方法组合在同一视图层次中时,圆角未正确对齐,如下所示: 可使用以下代码再现此简化示例: @interface ViewController : UIViewController @end @implementation ViewController -
cornerRadius
属性将圆角应用于视图图层。当视图只需要圆角的顶角或底角时,我会应用带有贝塞尔路径的遮罩(使用贝塞尔路径WithRoundedRect: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
属性并不能解决问题。我想了解为什么会发生这种情况。一个可能的解决方法是始终应用bezier路径遮罩,而不是简单地设置角半径,但这感觉太过分了。本网站做了很好的解释:(简而言之,这是一个仅限iOS7的东西)
有关更多说明,请参见:
同样有趣的是: