cocos2d-iphone – 在Coco2d中滑动
发布时间:2020-12-14 17:30:13 所属栏目:百科 来源:网络整理
导读:我试图让刷卡工作为Cocos2d最新版本这里是我的代码: -(void) setupGestureRecognizers { UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft)]; [swipeLeft setDirection:UISwipeGe
我试图让刷卡工作为Cocos2d最新版本这里是我的代码:
-(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”. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容