ios – SpriteKit:添加UIGestureRecognizer并检测哪个节点被刷新

前端之家收集整理的这篇文章主要介绍了ios – SpriteKit:添加UIGestureRecognizer并检测哪个节点被刷新前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在哪种方法中,我将一个UIGestureRecognizer添加到我的SKScene.如何检测哪个节点被刷新?这似乎不起作用:
-(id)initWithSize:(CGSize)size {    
    if (self = [super initWithSize:size]) {

    ...

    UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    recognizer.direction = UISwipeGestureRecognizerDirectionUp;
    [[self view] addGestureRecognizer:recognizer];

    }
    return self;
}

解决方法

您可以在此方法添加UISwipeGestureRecognizer:
- (void)didMoveToView:(SKView *)view
{
    UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
    recognizer.direction = UISwipeGestureRecognizerDirectionUp;
    [[self view] addGestureRecognizer:recognizer];
}

这就是您如何检测哪个SKNode被刷新:

- (void)handleSwipe:(UISwipeGestureRecognizer *)sender
{
    if (sender.state == UIGestureRecognizerStateEnded)
    {
        CGPoint touchLocation = [sender locationInView:sender.view];
        touchLocation = [self convertPointFromView:touchLocation];
        SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:touchLocation];

        NSLog(@"%@",touchedNode);
    }
}
原文链接:https://www.f2er.com/iOS/336237.html

猜你在找的iOS相关文章