我试图让刷卡工作为Cocos2d最新版本这里是我的代码: -(void) setupGestureRecognizers { UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft)]; [swipeLeft se
-(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”.
