SpriteKit 手势识别器 [英] SpriteKit Gesture Recognizer

查看:57
本文介绍了SpriteKit 手势识别器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的精灵套件游戏中使用手势识别器,我写了这段代码

Hi I'm trying to use gesture recognizer with my sprite kit game , i wrote this code

@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

我认为一切都很好,但我的手势不起作用,我没有错误消息,是否缺少某些内容我应该添加到我的应用程序中,或者我应该添加一些内容到我的视图控制器,或者你可以伙计们建议我一个 totorial 向我展示如何在 sprite kit 中使用手势,

i think every thing is ok but my gesture won't work and i don't have an error message, is there something missing i should add to my app , or should i add something to my view controller , or can you guys suggest me a totorial shows me how to use gestures with sprite kit ,

推荐答案

试试这个代码,它对我有用.你打错了,我猜...

Try this code, it works for me. You had a typo, I guess...

 @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

这篇关于SpriteKit 手势识别器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆