2012-08-11 66 views
4

我試圖給我的CAShapeLayer的路徑添加一個子路徑,但這些更改不會顯示,除非我首先將nil分配到該路徑,然後我將myPath重新分配給CAShapeLayer。我試過setNeedsRedisplay但它不起作用。如何在修改路徑時重繪CAShapeLayer?

這是LoadView其中myPathshapeLayer是屬性的代碼:

// The CGMutablePathRef 
CGPathMoveToPoint(myPath, nil, self.view.center.x, self.view.center.y); 
CGPathAddLineToPoint(myPath, nil, self.view.center.x + 100.0, self.view.center.y - 100.0); 

// The CAShapeLayer 
shapeLayer = [CAShapeLayer layer]; 
shapeLayer.path = myPath; 
shapeLayer.strokeColor = [UIColor greenColor].CGColor; 
shapeLayer.lineWidth = 2.0; 
[self.view.layer addSublayer:shapeLayer]; 

這是,例如,在我執行更改到的路徑。我只需添加一個新的子路徑,以myPath

- (void)handleOneFingerSingleTapGesture:(UIGestureRecognizer *)sender 
{ 
    // I add a new subpath to 'myPath' 
    CGPathMoveToPoint(myPath, nil, self.view.center.x, self.view.center.y); 
    CGPathAddLineToPoint(myPath, nil, self.view.center.x + 100.0, self.view.center.y + 100.0); 

    // I must do this to show the changes 
    shapeLayer.path = nil; 
    shapeLayer.path = myPath; 
} 

任何人知道如何解決這個問題?我正在使用iOS 5.0。

回答

-1

您必須致電[shapeLayer didChangeValueForKey:@"path"]才能讓圖層重新呈現自身。

+2

不IOS 8.3至少工作 – 2015-05-27 11:25:59

0

在修改路徑之前,您應該重新設置路徑超時刷新路徑繪圖的方式,通過removeAllPoints。

在Objective C:

[path removeAllPoints]; 
[path moveToPoint:p]; 
[path addLineToPoint:CGPointMake(x, y)]; 

shapeLayer.path = path.CGPath; 

在夫特4:

path.removeAllPoints() 
path.move(to: p1) 
path.addLine(to: p2) 

shapeLayer.path = path.cgPath