2017-03-19 85 views
0
for (CALayer *layer in self.view.layer.sublayers) { 
    if ([layer isKindOfClass:[CAShapeLayer class]]) { 
     CAShapeLayer *copy = (CAShapeLayer *)layer; 

     if ([copy.name isEqualToString:@"datumSideLayer"]) { 
      //get start position, get height, get width 

      CGPoint startPos = copy.frame.origin; 
      float width = copy.frame.size.width; 
      float height = copy.frame.size.height; 

      CGPoint startPoint = CGPointMake(startPos.x + (width/2), startPos.y); 
      CGPoint endPoint = CGPointMake(startPos.x + (width/2), startPos.y + height); 

      NSLog(@"%.1f:%.1f, %.1f:%.1f", startPoint.x, startPoint.y, endPoint.x, endPoint.y); 

      [self drawDashedLineFrom:startPoint toEnd:endPoint inView:_thicknessUI]; 

      break; 

     } 
    } 
} 

NSLog輸出爲0.0:0.0, 0.0:0.0嵌套CAShapeLayer§獲取來源?

它所嵌套的形狀圖層作爲屬性強烈實例化。但是,我無法抓住框架的原點,寬度或高度。

編輯:進一步挖掘後,我沒有框架設置爲我的CAShapeLayer - 因爲它是由路徑繪製。

但是,我現在怎麼能適應這個,所以它的起源可以從路徑的開始抓取?

[path moveToPoint:CGPointMake(self.view.frame.size.width/2 - _plateLengthPX, tenPercent)]; // datum side start 
[path addLineToPoint:CGPointMake(self.view.frame.size.width/2 - capStartWidthPX - rootGapPX, tenPercent)]; // weld(cap) start 
[path addLineToPoint:CGPointMake(self.view.frame.size.width/2 - rootGapPX, tenPercent + _plateHeightPX - rootPrepHeightPX)]; // weld(root prep) start 
[path addLineToPoint:CGPointMake(self.view.frame.size.width/2 - rootGapPX, tenPercent + _plateHeightPX)]; // weld(root prep) end 
[path addLineToPoint:CGPointMake(self.view.frame.size.width/2 - _plateLengthPX, tenPercent + _plateHeightPX)]; // datum side end 
[path addLineToPoint:CGPointMake(self.view.frame.size.width/2 - _plateLengthPX, tenPercent + _plateHeightPX/2 + 10)]; // datum side end 
[path addLineToPoint:CGPointMake(self.view.frame.size.width/2 - _plateLengthPX + 15, tenPercent + _plateHeightPX/2 + 5)]; // datum side end 
[path addLineToPoint:CGPointMake(self.view.frame.size.width/2 - _plateLengthPX - 15, tenPercent + _plateHeightPX/2 - 5)]; // datum side end 
[path addLineToPoint:CGPointMake(self.view.frame.size.width/2 - _plateLengthPX, tenPercent + _plateHeightPX/2 - 10)]; // datum side end 

_datumLayer = [CAShapeLayer layer]; 
_datumLayer.name = @"datumSideLayer"; 
_datumLayer.path = [path CGPath]; 
_datumLayer.strokeColor = [_lineColor CGColor]; 
_datumLayer.lineWidth = 2.0; 
_datumLayer.fillColor = [_shaderColor CGColor]; 

[self.view.layer addSublayer:_datumLayer]; 

回答

0

好吧我設法深入挖掘,找到我的問題的原因。

原因是因爲沒有設置CAShapeLayer的框架和原點,因此您不能使用§GET來檢索它們的值。但是,在實例化屬性時,可以設置相對於所用路徑的位置&。

CGRect pathRect = CGPathGetPathBoundingBox([path CGPath]); 
_datumLayer.bounds = pathRect; 
_datumLayer.position = CGPointMake(CGRectGetMidX(pathRect), CGRectGetMidY(pathRect));