2011-02-12 65 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