gpt4 book ai didi

ios - 为什么应用 bezierPathWithRoundedRect 蒙版会产生与设置图层的 cornerRadius 不同的结果?

转载 作者:搜寻专家 更新时间:2023-10-30 20:17:57 25 4
gpt4 key购买 nike

我通常使用 cornerRadius 属性将圆角应用于 View 层。当 View 只需要圆顶角或底角时,我改为应用带有贝塞尔曲线路径的 mask (使用 bezierPathWithRoundedRect:byRoundingCorners:cornerRadii:)。当这两种方法组合在同一 View 层次结构中时,圆角未正确对齐,如下图所示:

Difference in rounded corners

可以使用以下代码重现此简化示例:

@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 属性并没有解决问题。我想了解为什么会这样。一种可能的解决方法是始终应用​​贝塞尔曲线路径掩码,而不是简单地设置拐角半径,但这感觉有点矫枉过正。

最佳答案

这个网站很好地解释了:http://www.paintcodeapp.com/blogpost/code-for-ios-7-rounded-rectangles (简而言之,这是 iOS7 独有的东西)

有关额外的说明,请参阅: http://www.mani.de/backstage/?p=483

关于ios - 为什么应用 bezierPathWithRoundedRect 蒙版会产生与设置图层的 cornerRadius 不同的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22453095/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com