2011-02-12 58 views
1

我不明白爲什麼一個CAShapeLayer並不則hitTestCAShapeLayer則hitTest觸摸

迴應此功能總是轉到//觸摸超出

如何檢測在CAShapeLayer觸摸?

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

    currentPoint = [[touches anyObject] locationInView:self]; 

    for (CAShapeLayer *layer in self.layer.sublayers) {  

     if(layer == shapeLayer) { 

      if([layer hitTest:currentPoint]) 
      { 
       // touche is on the layer 
      } 
      else { 
       // touche is outside 
      } 

     } 

    }  

} 

回答

6

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

目標是擊中測試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 
    } 

} 
+0

效果很好。感謝分享!。 – Vignesh 2011-10-11 04:37:58