【问题标题】:add UIBezierPath to UIView将 UIBezierPath 添加到 UIView
【发布时间】:2015-12-19 17:24:23
【问题描述】:

我有一个带有布局约束(宽度:400,高度:180)的 UIView,我想在视图中添加一个贝塞尔曲线。

我的贝塞尔曲线是这样的:

UIBezierPath* bezierPath = [UIBezierPath bezierPath];

// origin, height | left border
[bezierPath moveToPoint: CGPointMake(0, 20)];

// origin, height | left border
[bezierPath addLineToPoint: CGPointMake(0, _donationView.frame.size.height)];

// height, width | bottom border
[bezierPath addLineToPoint: CGPointMake(_donationView.frame.size.width , _donationView.frame.size.height)];

[bezierPath addLineToPoint: CGPointMake(_donationView.frame.size.width, 20)];

[bezierPath addLineToPoint: CGPointMake(_donationView.frame.size.width - 20, 20)];

[bezierPath addLineToPoint: CGPointMake(_donationView.frame.size.width - 30, 15)];

[bezierPath addLineToPoint: CGPointMake(_donationView.frame.size.width - 40, 20)];

[bezierPath addLineToPoint: CGPointMake(0, 20)];

[bezierPath closePath];
[UIColor.grayColor setFill];
[bezierPath fill];
[UIColor.blackColor setStroke];
bezierPath.lineWidth = 1;
[bezierPath stroke];

创建贝塞尔曲线后,我传递给:

CAShapeLayer *shapeView = [[CAShapeLayer alloc] initWithLayer:_donationView.layer];
[shapeView setPath:bezierPath.CGPath];

_donationView.layer.mask = shapeView;

但是贝塞尔曲线转到这个 CGRect(0, 0, 240, 128);

我怎样才能获得 (0,0,400, 180) 的 CGRect?

谢谢大家

【问题讨论】:

    标签: ios objective-c uiview uibezierpath


    【解决方案1】:

    问题是您这样做太早了,在donationView 达到其最终大小之前。因此,当您阅读它的frame.size 时,您会得到错误的结果。您需要将掩码的添加推迟到 布局发生之后。

    如果donationView稍后重新调整大小,您也可能会遇到更多问题。遮罩层(或任何层)不参与自动布局是很烦人的。因此,如果 donationView 被调整大小,您将不得不移除此遮罩并创建一个具有正确大小的新遮罩,然后再次添加它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-23
      • 2021-09-27
      • 1970-01-01
      • 2015-03-12
      • 2013-08-08
      • 1970-01-01
      相关资源
      最近更新 更多