-(voID) setupGestureRecognizers { UISwipeGestureRecognizer *swipeleft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeleft)]; [swipeleft setDirection:UISwipeGestureRecognizerDirectionleft]; [swipeleft setNumberOftouchesrequired:1]; [[[CCDirector sharedDirector] openGLVIEw] addGestureRecognizer:swipeleft];}
它完全没有检测到滑动!
更新1:
我将代码更新为以下内容,但仍然没有检测到滑动.
-(voID) setupGestureRecognizers { UISwipeGestureRecognizer *swipeleft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeleft)]; [swipeleft setDirection:UISwipeGestureRecognizerDirectionleft]; [swipeleft setNumberOftouchesrequired:1]; [[[[CCDirector sharedDirector] openGLVIEw] window] setUserInteractionEnabled:YES]; [[[CCDirector sharedDirector] openGLVIEw] setUserInteractionEnabled:YES]; [[[CCDirector sharedDirector] openGLVIEw] addGestureRecognizer:swipeleft];}我也尝试过这项工作,但我发现了一种更简单,也更好的控制方法.
所以例如,如果你想要检测左边的滑动我会这样跟随.
在你的类的接口中声明两个变量
CGPoint firsttouch;CGPoint lasttouch;
在init方法中执行你的类启用touches
self.istouchEnabled = YES;
3.将这些方法添加到您的班级
-(voID)cctouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSSet *alltouches = [event alltouches]; UItouch * touch = [[alltouches allObjects] objectAtIndex:0]; CGPoint location = [touch locationInVIEw: [touch vIEw]]; location = [[CCDirector sharedDirector] convertToGL:location]; //Swipe Detection Part 1 firsttouch = location;}-(voID) cctouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { NSSet *alltouches = [event alltouches]; UItouch * touch = [[alltouches allObjects] objectAtIndex:0]; CGPoint location = [touch locationInVIEw: [touch vIEw]]; location = [[CCDirector sharedDirector] convertToGL:location]; //Swipe Detection Part 2 lasttouch = location; //Minimum length of the swipe float swipeLength = ccpdistance(firsttouch,lasttouch); //Check if the swipe is a left swipe and long enough if (firsttouch.x > lasttouch.x && swipeLength > 60) { [self doStuff]; }}
如果发生左滑动,则调用方法“doStuff”.
总结以上是内存溢出为你收集整理的cocos2d-iphone – 在Coco2d中滑动全部内容,希望文章能够帮你解决cocos2d-iphone – 在Coco2d中滑动所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)