最近一直琢磨在Cocos2d里添加手势的功能,找了一些资料加上自己的理解,整理出了三种方法和大家分享。
第一种,很简单,就是知易cocos2d-iPhone教程-04所介绍的(其实这并不是真正的手势,只是也能实现部分手势功能而已),代码如下:
1) 单击、双击处理
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//Get all the touches.
NSSet *allTouches = [event allTouches];
//Number of touches on the screen
switch ([allTouches count])
{
case 1:
{
//Get the first touch.
UITouch *touch = [[allTouches allObjects] objectAtIndex:0];
switch([touch tapCount])
{
case 1://Single tap
// 单击!!
break;
case 2://Double tap.
// 双击!!
break; }
}
break;
}
}
}
2) 两个指头的分开、合拢手势。
//计算两个点之间的距离函数
- (CGFloat)distanceBetweenTwoPoints:(CGPoint)fromPoint toPoint:(CGPoint)toPoint
{
float x = toPoint.x - fromPoint.x;
float y = toPoint.y - fromPoint.y;
return sqrt(x * x + y * y);
}
//记录多触点之间的初始距离
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSSet *allTouches = [event allTouches];
switch ([allTouches count])
{
case 1: { //Single touch
break;}
case 2: { //Double Touch
//Track the initial distance between two fingers.
UITouch *touch1 = [[allTouches allObjects] objectAtIndex:0];
UITouch *touch2 = [[allTouches allObjects] objectAtIndex:1];
initialDistance = [self distanceBetweenTwoPoints:[touch1 locationInView:[self view]] toPoint:[touch2 locationInView:[self view]]];
}
break;
default:
break;
}
}
//两个指头移劢时,判断是分开还是合拢
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSSet *allTouches = [event allTouches];
switch ([allTouches count])
{
case 1:
break;
case 2:{
UITouch *touch1 = [[allTouches allObjects] objectAtIndex:0];
UITouch *touch2 = [[allTouches allObjects] objectAtIndex:1];
//Calculate the distance between the two fingers.
CGFloat finalDistance = [self distanceBetweenTwoPoints: [touch1 locationInView:[self view]] toPoint:[touch2 locationInView:[self view]]];
//Check if zoom in or zoom out.
if(initialDistance > finalDistance) {
NSLog(@"Zoom Out"); // 合拢!!
}
else {
NSLog(@"Zoom In"); // 分开!!
}
}
break;
}
}
第二种,是在Cocoa China中找的一种办法,它的原理是通过修改CCLayer,CCNode两个Cocos2d类的源码实现手势支持:
1.首先要修改两个Cocos2d类的源码分别为CCLayer,CCNode
2.增加手势类源码 CCGestureRecognizer
以上三个类的源码,可以在我的资源中找着(地址:http://download.csdn.net/detail/wangqiuyun/4460442),(记住CCLayer与CCNode要覆盖原来的文件)
CCGestureRecognizer.h与.m要拷贝到当前工程的libs/cocos2d/Platforms/iOS目录下
在工程文件中加入CCGestureRecognizer.h与.m
3.完成以上工作后所有Node的子类中都可以使用手势了,如在HelloWorld工程中:
1)修改HelloWorldLayer.m中的init方法加入以下代码:
//定义响应的手势类(支持所有UI手势)
UILongPressGestureRecognizer * longPress = [[[UILongPressGestureRecognizer alloc] init] autorelease];
longPress.minimumPressDuration = 0.5f;
longPress.allowableMovement = 5.0f;
//将UI手势对象longPress作为CCGestureRecognizer类的初始化参数
//@selector(longPress:node:) 为响应手势的触发方法
CCGestureRecognizer * rescognizer = [CCGestureRecognizer CCRecognizerWithRecognizerTargetAction:longPress target:self action:@selector(longPress:node:)];
//设置手势类的代理
rescognizer.delegate = self;
//为self (当前为CCLayer对象)注册手势
[self addGestureRecognizer:rescognizer];
//必须设置self可接收Touch事件
self.isTouchEnabled = YES;
2)另需要增加响应方法的实现:
-(void) longPress:(UIGestureRecognizer *) recognizer node:(CCNode *) node
{
CCLOG(@"%s",__FUNCTION__);
}
3)需要支持协议UIGestureRecognizerDelegate 头文件如下:
#import "cocos2d.h"
// HelloWorldLayer
@interface HelloWorldLayer : CCLayer<UIGestureRecognizerDelegate>
{
}
// returns a CCScene that contains the HelloWorldLayer as the only child
+(CCScene *) scene;
-(void) longPress:(UIGestureRecognizer *) recognizer node:(CCNode *) node;
@end
第三种,自己琢磨的一种办法,个人感觉也还不错。
1)默认情况下面,cocos2d 模板并没有在AppDelegate里面包含一个RootViewController的属性,因此必须手动添加一个:
跳转到AppDelegate.h文件,并添加下面的代码:
@property (nonatomic, retain) RootViewController*viewController;
然后跳转到AppDelegate.m,@synthesize之:
@synthesize viewController;
2)在场景或者层中m文件中,#import "AppDelegate.h"
+(id) scene
{
//给层添加手势支持
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
HelloWorld *layer = [HelloWorld node];
UIPanGestureRecognizer *gestureRecognizer = [[[UIPanGestureRecognizer alloc] initWithTarget:layer action:@selector(handlePanFrom:)] autorelease];
AppDelegate *delegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
[delegate.viewController.view addGestureRecognizer:gestureRecognizer];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
//手势识别函数
- (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
CGPoint touchLocation = [recognizer locationInView:recognizer.view];
touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];
//实现的效果..
} else if (recognizer.state == UIGestureRecognizerStateChanged) {
CGPoint translation = [recognizer translationInView:recognizer.view];
translation = ccp(translation.x, -translation.y);
//实现的效果..
[recognizer setTranslation:CGPointZero inView:recognizer.view];
} else if (recognizer.state == UIGestureRecognizerStateEnded) {
//实现的效果..
}
}
以上是本人总结的三种在Cocos2d里添加手势支持的方法,不妥之处欢迎各位指教!