【问题标题】:iOS how to achieve UIButton's like behavior for nodes in Sprite Kit?iOS 如何为 Sprite Kit 中的节点实现 UIButton 的类似行为?
【发布时间】:2015-06-23 23:01:37
【问题描述】:

我希望我的一些 Sprite Kit 节点表现得像 UIButtons。我尝试了两种方法:

1) 使用touchesBegan: - 如果用户很小心,这会起作用,但似乎会触发多次,比我禁用交互的速度要快,导致按钮可以被多次激活:

   spriteNode.userInteractionEnabled = YES;

//causes the following to fire when node receives touch
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

        self.userInteractionEnabled = NO;

        DLog(@"Do node action");
        self.userInteractionEnabled = YES;


    }

2)我切换到Kobold-Kit 作为 iOS Sprite Kit 之上的层。它允许我做的一件事是将button - like behavior 添加到任何节点。但是,我遇到了一个问题,如果我有 2 个按钮堆叠在一起,点击顶部按钮会同时激活这两个按钮。在这种情况下,布尔标志可以防止重复交互。我在 KKScene 中跟踪了堆叠按钮一起触发此调用的问题:

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];

    for (id observer in _inputObservers)
    {
        if ([observer respondsToSelector:@selector(touchesBegan:withEvent:)])
        {
            [observer touchesBegan:touches withEvent:event];
        }
    }
}

场景只是向场景中的所有节点发送通知。这会导致堆叠在一起的按钮行为一起触发。

有没有一种方法或示例可以显示如何正确排列精灵节点以允许类似于 UIButton 的行为,其中我只能激活顶部按钮,并且每次激活都会短暂禁用按钮之后的时间?

【问题讨论】:

  • 使用选项 1,如果您使用本地布尔值而不是 userInteractionEnabled,会发生什么,可能会更快?
  • 我都试过了,似乎touchesBegan: 可以忽略两者。

标签: ios objective-c uibutton sprite-kit kobold-kit


【解决方案1】:
@property (nonatomic, strong) UITapGestureRecognizer *tapGesture;

self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureStateChanged:)];
self.tapGesture.delegate = self;
[self.view addGestureRecognizer:self.tapGesture];


- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer == self.tapGesture) {
        return CGRectContainsPoint(self.neededSprite.frame, [self convertPoint:[sender locationInView:self.view] toNode:self]);
    }
    return YES;
}



 - (void)tapGestureStateChanged:(UITapGestureRecognizer *)sender
    {
        if (sender.state == UIGestureRecognizerStateRecognized) {
/* do stuff here */
    }
    }

【讨论】:

  • 问题是关于Sprite Kit节点的,它们不是UIView的后代。
  • 然后...所以?场景有它自己的视图属性,这个解决方案在我的精灵套件游戏项目中很有效,在这里使用 UIGestureRecognizers 没问题
  • 我会看看是否可以使用识别的手势重写用于检测触摸的kobold kit场景方法。这可能会解决堆叠问题
【解决方案2】:

SpriteKit Programming Guide 建议使用 SKNode 名称来控制您在触摸时想要实现的行为。 “使用动作来动画场景”一节中的示例覆盖了 touchesBegan:withEvent: 方法,并且仅在名称不为 nil 时运行。完成后,只需重设名称,以便您可以捕捉到下一次触摸。

- (void)touchesBegan:(NSSet *) touches withEvent:(UIEvent *)event {
    SKNode *helloNode = [self childNodeWithName:@"helloNode"];
    if (helloNode != nil) {
        helloNode.name = nil;
        SKAction *moveUp = [SKAction moveByX: 0 y: 100.0 duration: 0.5];
        SKAction *zoom = [SKAction scaleTo: 2.0 duration: 0.25];
        SKAction *pause = [SKAction waitForDuration: 0.5];
        SKAction *fadeAway = [SKAction fadeOutWithDuration: 0.25];
        SKAction *remove = [SKAction removeFromParent];
        SKAction *moveSequence = [SKAction sequence:@[moveUp, zoom, pause, fadeAway, remove]];
        [helloNode runAction: moveSequence];
    }
}

另一个想法是重新启用与 touchesEnded 的用户交互。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多