【问题标题】:SpriteKit Gesture RecognizerSpriteKit 手势识别器
【发布时间】:2015-05-19 14:38:28
【问题描述】:

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

@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 手势,

【问题讨论】:

  • 为什么要删除 GestureRecognizer?有必要吗?
  • @LinusG。我猜是因为场景转换。那些 swipeRight 和 swipeLeft 方法是在那个场景中定义的。并且场景转换后识别器不会被移除,所以应用会崩溃。

标签: objective-c xcode6 sprite-kit uigesturerecognizer


【解决方案1】:

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

 @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

【讨论】:

  • 兄弟,非常感谢,手势效果很好,所以我认为如果我使用轻击手势识别器和滑动手势一样,没有问题?
  • 不客气,很高兴它对您有所帮助...您可以通过这种方式在 SKView 上使用任何手势识别器。
  • @Omar 再次查看代码,我为您添加了一个点击识别器的示例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-08
  • 1970-01-01
  • 2023-03-04
  • 2019-03-27
相关资源
最近更新 更多