【发布时间】:2023-05-25 06:38:01
【问题描述】:
我想创建一个蒙版路径,其最终输出将是一个带有凹面的角。
我使用-bezierPathWithRoundedRect:byRoundingCorners:cornerRadii: 方法创建了一个UIBezierPath 并指定只需要圆角(在本例中为UIRectCornerBottomLeft),我认为通过反转这个路径,我应该首先得到会被削减的东西。
为此,我正在尝试-bezierPathByReversingPath,但它似乎没有任何区别(有或没有,因为我得到的是正常的贝塞尔路径,而不是相反的)
这是我迄今为止尝试过的:
UIView *vwTest = [[UIView alloc] init];
[vwTest setFrame:CGRectMake(100.0f, 100.0f, 100.0f, 100.0f)];
[vwTest setBackgroundColor:[UIColor redColor]];
[self.view addSubview:vwTest];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:vwTest.bounds
byRoundingCorners:UIRectCornerBottomLeft
cornerRadii:CGSizeMake(50.0f, 50.0f)];
//get reverse path but doesn't seem to work as I think it should
maskPath = [maskPath bezierPathByReversingPath];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
[maskLayer setFrame:vwTest.bounds];
[maskLayer setPath:maskPath.CGPath];
[vwTest.layer setMask:maskLayer];
[vwTest.layer setMasksToBounds:YES];
[vwTest setNeedsDisplay];
根据下图,我想要实现的是显示UIView 的红色部分并消除该区域的其余部分。
目前,我在子视图上做子视图之类的事情,并将一个子视图的颜色与主视图的背景颜色同步(很糟糕,但至少我得到了所需的输出):
[self.view setBackgroundColor:[UIColor lightGrayColor]];
UIView *vwTest = [[UIView alloc] init];
[vwTest setFrame:CGRectMake(100.0f, 100.0f, 100.0f, 100.0f)];
[vwTest setBackgroundColor:[UIColor redColor]];
[self.view addSubview:vwTest];
UIView *vwPatch = [[UIView alloc] init];
[vwPatch setFrame:vwTest.bounds];
[vwPatch setBackgroundColor:vwTest.superview.backgroundColor];
[vwTest addSubview:vwPatch];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:vwPatch.bounds
byRoundingCorners:UIRectCornerBottomLeft
cornerRadii:CGSizeMake(50.0f, 50.0f)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
[maskLayer setFrame:vwPatch.bounds];
[maskLayer setPath:maskPath.CGPath];
[vwPatch.layer setMask:maskLayer];
[vwPatch.layer setMasksToBounds:YES];
[vwPatch setNeedsDisplay];
有人,请指点我正确的方向。
【问题讨论】:
-
我认为您需要手动绘制路径,然后使用
addArcWithCenter:radius:startAngle:endAngle:clockwise:。你能在你的问题中发布你想要的示例图片吗?它会帮助你回答:) -
我尝试了
-bezierPathWithArcCenter:radius:startAngle:endAngle:clockwise:,但也无法到达任何地方。无论如何...我会发布一张图片并将其添加到问题中。 -
@Rich:好的,我添加了一张图片......现在,我将研究你的答案,了解如何实现这一点。
标签: ios objective-c uiview uibezierpath cashapelayer