[英]A CALayer subLayer is being drawn with different frame origin. Bounds issue
I add a CAShapeLayer as subLayer to self.layer in a given view that way: 我以这种方式在给定视图中将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;
But, can't understand why, the shape is drawn under the height specified in balloonRect , it appears to be 110 px instead of 55 as it should. 但是,不明白为什么,形状是在balloonRect中指定的高度下绘制的,它看起来像是 110像素,而不是应该的55像素。 Strangely if I add the line
奇怪的是,如果我添加行
balloonFrame.bounds = _balloonRect;
after setting the layer's frame everything seems to be fine, what is happening precisely? 设置图层框架后,一切似乎都很好,到底发生了什么? why do I have to set the bounds if they're for the layer himself?
如果边界是图层本身,为什么还要设置边界?
The height of balloonRect
is not 55, it's 118. CGRectMake goes (x-top-left, y-top-left, width, height)
. balloonRect
的高度不是55,而是118。CGRectMake去了(x-top-left, y-top-left, width, height)
。
Changing the bounds
of something so as to have non-zero x-top-left
and y-top-left
shifts its drawing origin, so you are then shoving the drawing up and to the left (without actually changing the size of the layer). 更改某事物的
bounds
以使其非零的x-top-left
和y-top-left
会移动其图形原点,因此您将图形向上和向左推(实际上并未更改图层的大小) 。
You might be helped by reading the sections in my book about frame and bounds, starting here: http://www.apeth.com/iOSBook/ch14.html#_frame 从我的书中有关边框和界限的部分开始,可能会对您有所帮助: http : //www.apeth.com/iOSBook/ch14.html#_frame
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.