2014-02-06 25 views
1

我有我的第一次經驗CALayers,我毫不懷疑地做了一件非常簡單的事情,非常錯誤。添加CAShapeLayer作爲不可見的子圖層

以下代碼是從UIView擴展而來的類。我正在訪問視圖的CALayer,這很好,並嘗試根據用戶觸摸所繪製的線條來附加子圖層。 (該CGPath

通過設置背景明亮的橙色,我可以看到我傳遞正確的CALayer到我的功能,但如果我嘗試並設置CAShapeLayer *line橙色 - 我似乎沒有能夠在屏幕上看到它。

我在想象我把兩個連接在一起錯了,或者錯過了定義某個尺寸或某種東西,但是我想嘗試一下新鮮事物。任何人都可以設置我嗎?

(void)makeLineLayer:(CALayer *)layer{ 
    CAShapeLayer *line = [CAShapeLayer layer]; 
    //This line sets the view to orange - so I know 'layer' is pointing to the right thing 
    //layer.backgroundColor = [UIColor orangeColor].CGColor; 
    UIBezierPath *linePath=[UIBezierPath bezierPath]; 
    [linePath moveToPoint:self.lineStart]; 
    [linePath addLineToPoint:self.lineEnd]; 
    [linePath setLineWidth:60.0]; 

    line.path=linePath.CGPath; 
    //This below line is meant to show me where my sublayer is - I currently can't see it. 
    line.fillColor = [UIColor orangeColor].CGColor; 
    line.opacity = 1.0; 
    [layer addSublayer:line]; 
} 

回答

4

你的代碼沒有問題。

但是有一個問題很突出 - 考慮到你只有一點要移動,沒有什麼可以「填充」。

設置lineWidth沒有任何幫助,因爲線的path只把它看作是形成一個點到另一個,沒有考慮到UIBezierPathwidth線。

如果你想看到你的代碼做其實作品,添加這個,看到它:

UIBezierPath *linePath=[UIBezierPath bezierPath]; 
[linePath moveToPoint:self.lineStart]; 
[linePath addLineToPoint:self.lineEnd]; 

[linePath addLineToPoint:(CGPoint){250.0f, 350.0f}]; <<=== random points I chose. 

[linePath setLineWidth:60.0]; 

有樂趣。希望這可以幫助。

附錄,如果你想要的是顯示線與線而已,請添加以下內容:

line.strokeColor = [UIColor <YOUR_COLOR>].CGColor; 

然後你就可以刪除此我提到:

[linePath addLineToPoint:(CGPoint){250.0f, 350.0f}]; 
+1

非常棒的地方,謝謝。知道它必須是愚蠢的,它不夠複雜的代碼是複雜的!不,圖像最終沿着這條路走,所以我需要額外的點數,但是我可以計算出複製寬度爲60的值。感謝您的幫助! – Chris

相關問題