Reputation: 113
i created a project by using cocos2d,and now i want to use UISwipeGestureRecognizer for get the up/down/left/right, so how can i do? thanks a lot
Upvotes: 2
Views: 2261
Reputation: 607
In the .h file add this :
// Add inside @interface
UISwipeGestureRecognizer * _swipeLeftRecognizer;
UISwipeGestureRecognizer * _swipeRightRecognizer;
// Add after @interface
@property (retain) UISwipeGestureRecognizer * swipeLeftRecognizer;
@property (retain) UISwipeGestureRecognizer * swipeRightRecognizer;
In .m file add this :
// Add after @implementation
@synthesize swipeLeftRecognizer = _swipeLeftRecognizer;
@synthesize swipeRightRecognizer = _swipeRightRecognizer;
// Then add these new methods
- (void)onEnter {
self.swipeLeftRecognizer = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleLeftSwipe:)] autorelease];
_swipeLeftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[[[CCDirector sharedDirector] openGLView] addGestureRecognizer:_swipeLeftRecognizer];
self.swipeRightRecognizer = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightSwipe:)] autorelease];
_swipeRightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[[[CCDirector sharedDirector] openGLView] addGestureRecognizer:_swipeRightRecognizer];
}
- (void)onExit {
[[[CCDirector sharedDirector] openGLView] removeGestureRecognizer:_swipeLeftRecognizer];
[[[CCDirector sharedDirector] openGLView] removeGestureRecognizer:_swipeRightRecognizer];
}
// Add to dealloc
_swipeLeftRecognizer = nil;
[_swipeRightRecognizer release];
_swipeRightRecognizer = nil;
Hope it'll help
Upvotes: 7