【问题标题】:How to get CAShapeLayer's frame confront to its superview?如何让 CAShapeLayer 的框架与其父视图相对?
【发布时间】:2016-10-12 07:35:05
【问题描述】:

谁能帮我获取作为子图层添加到 UIImageView 中的 CAShapeLayer 框架?

以下是我的代码:

CAShapeLayer  *shapeLayer = [[CAShapeLayer alloc] init];
shapeLayer.fillColor = [[UIColor redColor] colorWithAlphaComponent:0.15].CGColor;
shapeLayer.strokeColor = [UIColor redColor].CGColor;
shapeLayer.lineWidth = 3.0;
shapeLayer.zPosition = 1;
[_imgBackground.layer insertSublayer:shapeLayer atIndex:1];

UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(50, 50)]; // left top point of rack
[path addLineToPoint:CGPointMake(100, 50)]; // right top point of rack
[path addLineToPoint:CGPointMake(100, 100)]; // right bottom point of rack
[path addLineToPoint:CGPointMake(50, 100)]; // left bottom point of rack
[path closePath]; // closing rectangle/path

shapeLayer.path = path.CGPath;

据我所知,我们可以从以下行获取框架:

CGRect bounds = CGPathGetBoundingBox(shapeLayer.path);

但它只根据self.view返回帧

我希望它的框架根据 UIImageView。

提前致谢。

【问题讨论】:

  • this你的情况吗?
  • 检查我的回答并告诉我..
  • 试试 CALayer。
  • 你可以使用convertRect

标签: ios objective-c uibezierpath cashapelayer


【解决方案1】:

嗯,我从詹姆斯的评论中得到了答案。以下行节省了我的时间:

CGRect bounds = [self.view convertRect:CGPathGetBoundingBox(shapeLayer.path) toView:_imgBackground];

来自this 参考。

【讨论】:

    猜你喜欢
    • 2019-10-04
    • 1970-01-01
    • 1970-01-01
    • 2014-10-07
    • 1970-01-01
    • 1970-01-01
    • 2011-02-24
    • 2021-10-24
    • 2021-11-17
    相关资源
    最近更新 更多