Cocos2d中添加手势支持的三种技巧
发布时间:2021-12-14 21:55:54 所属栏目:教程 来源:互联网
导读:最近一直琢磨在Cocos2d里添加手势的功能,找了一些资料加上自己的理解,整理出了三种方法和大家分享。 第一种,很简单,就是知易cocos2d-iPhone教程-04所介绍的(其实这并不是真正的手势,只是也能实现部分手势功能而已),代码如下: 1) 单击、双击处理 -
最近一直琢磨在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; } } ![]() (编辑:PHP编程网 - 黄冈站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |