我有一个UITapGestureRecognizer和一个UIPanGestureRecognizer在一个UIView和一个SKScene在它上面。pan手势识别器将一个SKNode从左向右移动,我希望Tap手势识别器检测到一个pan的子SKNode。平移操作很好,但我在检测抽头时遇到了问题-- Tap手势触发了相关的方法,但我不知道如何将坐标从视图转换到节点,以检测其中一个子节点中的抽头是否存在。
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);
}
}
}}
发布于 2015-03-18 16:33:34
最后,我需要从建议中再执行几个步骤--从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];发布于 2015-03-18 00:57:30
您应该使用convertPointFromView:将视图坐标转换为场景坐标
CGPoint touchLocationInView = [sender locationOfTouch:0 inView:sender.view];
CGPoint touchLocationInScene = [self convertPointFromView:touchLocationInView];然后,您可以检测到哪个标签节点被点击,
for (SKLabelNode *node in self.children) {
if ([node containsPoint:touchLocationInScene]) {
//This is where I want the tap to be detected.
}
}发布于 2015-03-17 23:10:12
我以前没有使用过SceneKit,但是从文档来看,您似乎需要使用SKView方法convertPoint:toScene:将手势识别器的点击坐标从视图坐标转换为场景坐标。然后,您需要点击测试场景中的节点,以确定哪个节点被点击。
https://stackoverflow.com/questions/29110944
复制相似问题