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