4

我有兩個CAShapeLayers insde UIView的主層。 CAShapeLayers具有複雜的形狀,我需要知道是否在形狀邊界內觸摸了一個點。另外,我需要知道哪個形狀被觸摸。containsPoint不適用於CAShapeLayer?

我正在嘗試containsPoint,但沒有任何工作。

+1

`CGPathContainsPoint()`是一個`CGPath`對象上工作的功能。它不關心(或者不知道)這個路徑是否是'CAShapeLayer`的一部分,或者是用Core Graphics調用手動繪製的。請告訴我們你的代碼。 – 2011-02-13 12:59:04

回答

7

將我的頭撞了兩天後,我能夠產生這個奇怪的代碼,看起來像是在工作!

目標是擊中測試CAShapeLayer。 CAShapeLayer正在屏幕上移動,所以形狀不在固定位置。點擊測試CGPath currentPoint並不簡單。

隨意添加任何輸入...

 
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

    CGPoint p = [[touches anyObject] locationInView:self]; 

    CGAffineTransform transf = CGAffineTransformMakeTranslation(-shapeLayer.position.x, -shapeLayer.position.y); 

    if(CGPathContainsPoint(shapeLayer.path, &transf, p, NO)){  

     // the touch is inside the shape 
    } 

}