SpriteKit手势识别器

4

你好,我正在尝试在我的Sprite Kit游戏中使用手势识别器,我编写了以下代码:

@interface GameScene() <UIGestureRecognizerDelegate>{

  UISwipeGestureRecognizer *swipeGestureLeft;
  ISwipeGestureRecognizer *swipeGestureRight;
}
@end

@implementation GameScene
-(id)initWithSize:(CGSize)size{

    if(self = [ super initWithSize:size]){

    }

    return  self;
}

-(void)didMoveToView:(SKView *)view{

    swipeGestureLeft = [[UIGestureRecognizer alloc]initWithTarget:self action:@selector(swipeLeft)];
    [swipeGestureLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
    [view addGestureRecognizer:swipeGestureLeft];

    swipeGestureRight = [[UIGestureRecognizer alloc]initWithTarget:self action:@selector(swipeRight)];
    [swipeGestureRight setDirection:UISwipeGestureRecognizerDirectionRight];
    [view addGestureRecognizer:swipeGestureRight];
}

- ( void ) willMoveFromView: (SKView *) view {

    [view removeGestureRecognizer: swipeGestureLeft ];
    [view removeGestureRecognizer: swipeGestureRight];
}

-(void)swipeLeft:(UISwipeGestureRecognizer*) recognizer{

    NSLog@"Left"'
}

-(void)swipeRight:(UISwipeGestureRecognizer*) recognizer{

    NSLog@"Right"'
}


@end

我认为一切都好,但是我的手势不起作用,而且我没有错误信息。是否还有什么我应该在我的应用程序中添加,或者我应该在我的视图控制器中添加一些内容?或者你们能否向我推荐一个教程,展示如何在Sprite Kit中使用手势。


你为什么要移除手势识别器?这是必要的吗? - LinusGeffarth
1
@LinusG。我猜是因为场景转换的缘故。那些swipeRight和swipeLeft方法是在那个场景中定义的。而且识别器在场景转换后不会被移除,所以应用程序会崩溃。 - Whirlwind
1个回答

5

试试这段代码,它对我有效。我猜你打错了一个字母......

 @interface GameScene() <UIGestureRecognizerDelegate>{

        UISwipeGestureRecognizer *swipeGestureLeft; 
        UISwipeGestureRecognizer *swipeGestureRight;
        UITapGestureRecognizer *doubleTapGesture;
    }

    @end

    @implementation GameScene

    -(void)didMoveToView:(SKView *)view {
        /* Setup your scene here */

        //You should use UISwipeGestureRecognizer instead of UIGestureRecognizer here.

        swipeGestureLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeLeft:)];
        [swipeGestureLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
        [view addGestureRecognizer:swipeGestureLeft];


        //Note that swipeRight has a parameter, so you  have to change swipeRight to swipeRight: to silent the compiler warning.
        swipeGestureRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeRight:)];
        [swipeGestureRight setDirection:UISwipeGestureRecognizerDirectionRight];
        [view addGestureRecognizer:swipeGestureRight];


        //double tap detection
        doubleTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapTap:)];
       [doubleTapGesture setNumberOfTapsRequired:2];
       [view addGestureRecognizer:doubleTapGesture];
    }

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        /* Called when a touch begins */


    }

    -(void)update:(CFTimeInterval)currentTime {
        /* Called before each frame is rendered */
    }

    - ( void ) willMoveFromView: (SKView *) view {


        [view removeGestureRecognizer: swipeGestureLeft ];

        [view removeGestureRecognizer: swipeGestureRight];

        [view removeGestureRecognizer: doubleTapGesture];
    }

    -(void)swipeLeft:(UISwipeGestureRecognizer*) recognizer{

        NSLog(@"Left");

    }

    -(void)swipeRight:(UISwipeGestureRecognizer*) recognizer{

        NSLog(@"Right");


    }

   -(void)tapTap:(UITapGestureRecognizer*) recognizer{

        NSLog(@"Tap tap");

    }

    @end

兄弟,非常感谢你,手势效果很好,所以我认为如果我像使用滑动手势一样使用点击手势识别器,应该没有问题吧? - Omar
不客气,很高兴能帮到你...你可以以这种方式在SKView上使用任何手势识别器。 - Whirlwind
@Omar,请再次查看代码,我为您添加了一个关于轻拍手势识别器的示例。 - Whirlwind

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接