我以这种方式在给定视图中将 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;
设置图层的框架后,一切似乎都很好,究竟发生了什么?如果它们是针对图层本身的,为什么我必须设置边界?