SpriteKit Gesture Recognizer

喜欢而已 提交于 2019-12-05 15:17:11

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!