您好我正在尝试使用手势识别器与我的精灵套件游戏,我写了这段代码
@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手势,
答案 0 :(得分:5)
试试这段代码,它对我有用。你猜错了...
@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