最近一直琢磨在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里添加手势支持的方法,不妥之处欢迎各位指教!
总结以上是内存溢出为你收集整理的Cocos2d中添加手势支持的三种方法全部内容,希望文章能够帮你解决Cocos2d中添加手势支持的三种方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)