2013-04-13 26 views
0

我添加CAShapeLayer作爲子在給定的視圖self.layer這樣:CALayer subLayer正在繪製不同的幀起源。邊界問題

//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 px而不是55。 奇怪的是,如果我添加行

balloonFrame.bounds = _balloonRect; 

設置層的框架後,一切似乎都很正常,什麼是正是發生了什麼?爲什麼我必須設置邊界,如果他們是爲了自己的圖層?

回答

0
  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