iOS如何实现UIButton';Sprite工具包中节点的行为类似于什么?
我希望我的一些精灵工具包节点的行为类似于UIButtons。我尝试了两种方法: 1) 使用iOS如何实现UIButton';Sprite工具包中节点的行为类似于什么?,ios,objective-c,uibutton,sprite-kit,kobold-kit,Ios,Objective C,Uibutton,Sprite Kit,Kobold Kit,我希望我的一些精灵工具包节点的行为类似于UIButtons。我尝试了两种方法: 1) 使用触摸开始:-如果用户很小心,这项功能会起作用,但似乎会多次触发,速度比我禁用交互的速度快,从而导致按钮能够被激活多次: spriteNode.userInteractionEnabled = YES; //causes the following to fire when node receives touch -(void)touchesBegan:(NSSet *)touches wit
触摸开始:
-如果用户很小心,这项功能会起作用,但似乎会多次触发,速度比我禁用交互的速度快,从而导致按钮能够被激活多次:
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) 我切换到iOS精灵套件的顶层。它允许我做的一件事就是添加到任何节点。然而,我遇到了一个问题,如果我有两个按钮堆叠在一起,点击顶部按钮会激活这两个按钮。在这种情况下,布尔标志可以防止重复交互。我跟踪了堆叠按钮一起触发的问题,并在场景中进行了调用:
-(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的行为,在UIButton中,我只能激活顶部按钮,每次激活后都会在短时间内禁用该按钮?建议使用SKNode名称来控制触摸时想要实现的行为。“使用动作设置场景动画”一节中的示例覆盖了touchesbeent:withEvent:method,并且仅在名称不为nil时运行。完成后,只需重置名称,以便捕捉下一次触摸
@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 */
}
}
- (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。对于选项1,如果使用本地布尔值而不是userInteractionEnabled,会发生什么情况?我尝试了这两种方法,而ToucheSStart:似乎可以忽略这两种方法。问题是关于精灵工具包节点,他们不是UIView的后代。而且。。。所以场景有自己的视图属性,这个解决方案在我的sprite kit游戏项目中非常有效,在这里使用UIgestureRecognitors是可以的。我将看看用于检测触摸的kobold kit场景方法是否可以使用识别的手势重写。这可以解决堆叠问题