【问题标题】:A CALayer subLayer is being drawn with different frame origin. Bounds issue正在使用不同的帧原点绘制 CALayer 子图层。界限问题
【发布时间】:2013-04-13 19:24:23
【问题描述】:

我以这种方式在给定视图中将 CAShapeLayer 作为 subLayer 添加到 self.layer:

//balloonrect's value is this one
_balloonRect = CGRectMake(0, 55, 239, 118);

CAShapeLayer *balloonFrame = [CAShapeLayer layer];
balloonFrame.cornerRadius = 15.0f;
balloonFrame.frame = _balloonRect;
balloonFrame.fillColor = _balloonColor.CGColor;
balloonFrame.strokeColor = _balloonStrokeColor.CGColor;
balloonFrame.lineWidth = 5.0f;
balloonFrame.lineCap = kCALineCapRound;
balloonFrame.lineJoin = kCALineJoinRound;
balloonFrame.masksToBounds = YES;
UIBezierPath *fillPath = [UIBezierPath bezierPathWithRoundedRect: CGRectInset( _balloonRect, balloonFrame.lineWidth, balloonFrame.lineWidth) cornerRadius: 15.0f];
balloonFrame.path = fillPath.CGPath;

但是,不明白为什么,形状是在 balloonRect 中指定的高度下绘制的,它看起来是 110 像素,而不是应有的 55 像素。 奇怪的是,如果我添加行

    balloonFrame.bounds = _balloonRect;

设置图层的框架后一切似乎都很好,究竟发生了什么?为什么我必须为图层自己设置边界?

【问题讨论】:

    标签: ios objective-c core-animation core-graphics calayer


    【解决方案1】:
    1. balloonRect 的高度不是 55,而是 118。CGRectMake 变为 (x-top-left, y-top-left, width, height)

    2. 更改某物的bounds 以使x-top-lefty-top-left 变为非零会移动其绘图原点,因此您将绘图向上和向左推(实际上不改变大小层)。

    阅读我书中关于框架和边界的部分可能会对您有所帮助,从这里开始:http://www.apeth.com/iOSBook/ch14.html#_frame

    【讨论】:

      猜你喜欢
      • 2010-11-21
      • 2020-07-19
      • 2023-03-25
      • 2012-06-03
      • 2021-10-24
      • 2014-11-23
      • 1970-01-01
      • 2012-02-17
      • 1970-01-01
      相关资源
      最近更新 更多