我使用 cocos2d 创建了一个项目,现在我想使用 UISwipeGestureRecognizer 来获取上/下/左/右,我该怎么办?多谢
问问题
2256 次
1 回答
7
在 .h 文件中添加:
// Add inside @interface
UISwipeGestureRecognizer * _swipeLeftRecognizer;
UISwipeGestureRecognizer * _swipeRightRecognizer;
// Add after @interface
@property (retain) UISwipeGestureRecognizer * swipeLeftRecognizer;
@property (retain) UISwipeGestureRecognizer * swipeRightRecognizer;
在 .m 文件中添加:
// 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;
希望它会有所帮助
于 2012-02-15T08:56:48.300 回答