objective-c - SpriteKit 手势识别器

标签 objective-c xcode6 sprite-kit uigesturerecognizer

您好,我正在尝试在我的 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

我认为一切正常,但我的手势不起作用,而且我没有收到错误消息,是否缺少我应该添加到我的应用程序中的东西,或者我应该添加一些东西到我的 View Controller 中,或者你可以伙计们建议我看一篇介绍如何使用 sprite kit 的手势,

最佳答案

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

 @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

关于objective-c - SpriteKit 手势识别器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30328769/

相关文章:

ios - 当应用程序等待审核时,我可以更改我的 bundle 显示名称(应用程序下方的文本)吗

objective-c - 地址簿联系人是否有 "uniqueID"类型的属性?

ios7 - NSLayoutConstraints 在 ios7 上崩溃但在 ios8 上没有

swift - 如何更改 SKScene 的呈现 View ?

swift - 关闭音效

android - 将图像转换为无符号字符*

ios - 使用 SpriteKit 创建一条直线

swift UIActivityIndi​​catorView .hidden = false 不工作

objective-c - Xcode 6.4 挂起 "Timed out waiting to acquire lock file for module ' AVFoundation'"for Release 配置

iOS Sprite Kit - SKSpriteNode 似乎没有物理体参与模拟