SpriteKit手势识别器

问题描述:

嗨,我试图用手势识别与我的精灵套件的游戏,我写了这个代码SpriteKit手势识别器

@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{ 

    [email protected]"Left"' 
} 

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

    [email protected]"Right"' 
} 


@end 

我觉得每一件事情是确定的,但我的手势将无法正常工作,我不有错误信息,是否有东西丢失我应该添加到我的应用程序,或者我应该添加一些东西到我的视图控制器,或者你们可以建议我一个totorial显示我如何使用手势与sprite套件,

+0

你为什么要删除GestureRecognizer?这是必要的吗? – LinusGeffarth

+1

@LinusG。我猜是因为场景过渡。那些swipeRight和swipeLeft方法在​​该场景中定义。在场景转换后识别器不会被删除,所以应用会崩溃。 – Whirlwind

试试这个代码,它适用于我。你有一个错字,我猜...

@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 
+0

非常感谢你,手势效果很好,所以我认为如果我使用轻击手势识别器作为我用于轻扫手势的相同方式没有问题? – Omar

+0

欢迎您,很高兴帮助您...您可以用这种方式在SKView上使用任何手势识别器。 – Whirlwind

+0

@Omar再次查看代码,我添加了一个轻击识别器的示例。 – Whirlwind