我在UIView上有一个UITapGestureRecognizer和一个UIPanGestureRecognizer,上面有一个SKScene.平移手势识别器从左向右移动SKNode,并且我希望Tap手势识别器检测到平移的SKNode的孩子.平移工作正常,但我在检测水龙头时遇到问题 – Tap Gesture会触发相关方法,但我不确定如何将坐标从视图转换到场景到节点以检测水龙头是否在其中一个子节点.
UIView(带手势)→SKScene→平移节点→平移节点的子节点
如何检查轻击手势的触摸坐标是否为任何给定的SKNode?
-(void)tapAction:(UITapGestureRecognizer*)sender{ if (sender.state == UIGestureRecognizerStateEnded) { // handling code CGPoint touchLocation = [sender locationOfTouch:0 inView:sender.view]; NSLog(@"TAP %@",NSStringFromCGPoint(touchLocation) ); for (SKLabelNode *node in _containerNode.children) { if ([node containsPoint:[node convertPoint:touchLocation fromNode:self.parent]]) { //This is where I want the tap to be detected. } CGPoint checkPoint = [node convertPoint:touchLocation fromNode:self.scene]; NSLog(@"CheckPoint %@",NSStringFromCGPoint(checkPoint) ); //NSLog(@"iterating nodes"); if ([node containsPoint:checkPoint]) { NSLog(@"touch match %@",node); } } }
}
解决方法
最后,我需要从建议的步骤中做更多的步骤 – 从SKView→SKScene转换到包含我正在测试的节点的SKNode.
CGPoint touchLocation = [sender locationOfTouch:0 inView:sender.view]; CGPoint touchLocationInScene = [[self.scene view] convertPoint:touchLocation toScene:self.scene]; CGPoint touchLocationInNode = [self.scene convertPoint:touchLocationInScene toNode:_containerNode];