Objective c SpriteKit手势识别器
嗨,我想在我的精灵套件游戏中使用手势识别器,我写了这段代码Objective c SpriteKit手势识别器,objective-c,xcode6,sprite-kit,uigesturerecognizer,Objective C,Xcode6,Sprite Kit,Uigesturerecognizer,嗨,我想在我的精灵套件游戏中使用手势识别器,我写了这段代码 @interface GameScene() <UIGestureRecognizerDelegate>{ UISwipeGestureRecognizer *swipeGestureLeft; ISwipeGestureRecognizer *swipeGestureRight; } @end @implementation GameScene -(id)initWithSize:(CGSize)size{
@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
@界面游戏场景(){
UISwipegestureRecognitor*swipeGestureLeft;
IsWipegestureRecognitor*swipeGestureRight;
}
@结束
@实现游戏场景
-(id)initWithSize:(CGSize)大小{
if(self=[super initWithSize:size]){
}
回归自我;
}
-(void)didMoveToView:(SKView*)视图{
swipeGestureLeft=[[UIgestureRecognitizer alloc]initWithTarget:self action:@selector(swipeLeft)];
[swipeGestureLeft设置方向:UISwipegestureRecognitizerDirectionLeft];
[查看AddGestureRecognitor:swipeGestureLeft];
swipeGestureRight=[[UIgestureRecognitizer alloc]initWithTarget:self action:@selector(SwiperRight)];
[swipeGestureRight设置方向:UISwipegestureRecognitizerDirectionRight];
[查看AddGestureRecognitor:swipeGestureRight];
}
-(void)willMoveFromView:(SKView*)视图{
[查看移除检测识别器:swipeGestureLeft];
[查看RemovegestureRecognitor:SwipegTestureRight];
}
-(void)swipeLeft:(UISwipegestureRecognitor*)识别器{
NSLog@“左”
}
-(void)swipeRight:(UISwipegestureRecognitor*)识别器{
NSLog@“正确”'
}
@结束
我想一切都没问题,但我的手势不起作用,我没有错误消息,我是否应该在我的应用程序中添加一些东西,或者我应该在我的视图控制器中添加一些东西,或者你们能给我推荐一本关于如何在精灵工具包中使用手势的教程,试试这段代码,它对我很有用。我猜你是打字错误了
@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
@界面游戏场景(){
UISwipegestureRecognitor*swipeGestureLeft;
UISwipegestureRecognitor*swipeGestureRight;
UITapGestureRecognitor*双击手势;
}
@结束
@实现游戏场景
-(void)didMoveToView:(SKView*)视图{
/*在这里设置场景*/
//您应该在此处使用UIWipegestureRecognitor,而不是UIgestureRecognitor。
swipeGestureLeft=[[UISwipeGestureRecognitizer alloc]initWithTarget:self action:@selector(swipeLeft:)];
[swipeGestureLeft设置方向:UISwipegestureRecognitizerDirectionLeft];
[查看AddGestureRecognitor:swipeGestureLeft];
//请注意,swipeRight有一个参数,因此必须将swipeRight更改为swipeRight:以使编译器警告静音。
swipeGestureRight=[[UISwipegestureRecognitizer alloc]initWithTarget:self action:@selector(SwiperRight:)];
[swipeGestureRight设置方向:UISwipegestureRecognitizerDirectionRight];
[查看AddGestureRecognitor:swipeGestureRight];
//双抽头检测
DoubleTap手势=[[UITapGestureRecognitizer alloc]initWithTarget:self-action:@selector(tapTap:)];
[DoubleTap手势设置需要的Tap数:2];
[查看添加手势识别器:双击手势];
}
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件{
/*当触摸开始时调用*/
}
-(无效)更新:(CFTimeInterval)currentTime{
/*在渲染每个帧之前调用*/
}
-(void)willMoveFromView:(SKView*)视图{
[查看移除检测识别器:swipeGestureLeft];
[查看RemovegestureRecognitor:SwipegTestureRight];
[查看RemovegestureRecognitor:双击手势];
}
-(void)swipeLeft:(UISwipegestureRecognitor*)识别器{
NSLog(@“左”);
}
-(void)swipeRight:(UISwipegestureRecognitor*)识别器{
NSLog(@“右”);
}
-(无效)轻触:(UITapgestureRecognitor*)识别器{
NSLog(“轻敲”);
}
@结束
为什么要删除手势识别器?有必要吗?@LinusG。我猜是因为场景转换。这些swipeRight和swipeLeft方法在该场景中定义。而且识别器在场景转换后不会被移除,所以应用程序会崩溃。兄弟,非常感谢,这个手势很好,所以我认为如果我使用轻拍手势识别器,就像我使用刷卡手势一样,没有问题吗?不客气,很高兴它有帮助……您可以用这种方式在SKView上使用任何手势识别器。@Omar再次检查代码,我为您添加了一个tap识别器的示例。