那么大概介绍了监听事件后,那么触屏中最关心的就应该是多触点啦;
- //-----获取多点触摸
- NSSet *allTouches = [event allTouches];
- UITouch *touchOne = [[allTouches allObjects]objectAtIndex:0];
- UITouch *touchTwo = [[allTouches allObjects]objectAtIndex:1];
- //...类推
获取多点狠简单,那么下面再将基本常用到的几个判断写下:1-判断用户单击还是双击(针对一个触点)
- if([allTouches count]==1) {
- UITouch *touchOne = [[allTouches allObjects]objectAtIndex:0];
- switch ([touchOne tapCount]) {
- case 1:
- //单击
- CCLOG(@"%@",@"单击");
- break;
- case 2:
- //双击
- CCLOG(@"%@",@"双击");
- break;
- }
- }
2-判断用户两个触点之间是合拢还是分开(针对两个触点)
- if([allTouches count]==2) {
- //适当修改处理,不能同时取,否则肯定一样的啦(可以一个在began 一个在end)
- UITouch *touchOne = [[allTouches allObjects]objectAtIndex:0];
- UITouch *touchTwo = [[allTouches allObjects]objectAtIndex:1];
- CGFloat *disFirst =[self distance:[touchOne locationInView:[self view]]
- todistance:[touchTwo locationInView:[self view]]];
- UITouch *touchOne = [[allTouches allObjects]objectAtIndex:0];
- UITouch *touchTwo = [[allTouches allObjects]objectAtIndex:1];
- CGFloat *disFinal =[self distance:[touchOne locationInView:[self view]]
- todistance:[touchTwo locationInView:[self view]]];
- if (disFirst>disFinal) {
- CCLOG(@"%@",@"合拢");
- }else{
- CCLOG(@"%@",@"分开");
- }
- }
这里我就粗略的写在一起,判定两个触点是否合拢其实就是用户刚触屏时记录两点之间的距离记做disFirst,然后在两个触点离开屏幕(或者移动事件中)的时候计算
当前的两个触点的距离disFinal,那么最后根据disFirst与disFinal距离关系就能知道是合拢还是分开;
(CCLOG 是cocos2d封装的打印方法,此种打印在编译发布正式游戏程序的时候是不会编译到程序中的,但是NSLOG会一直存在!要注意!)
最后给出两个函数,用于计算不同方式监听的函数中获取(转换)坐标的,因为cocos2d是openGL进行搭建的框架,所以需要坐标转换;
- +(CGPoint) locationFromTouches:(NSSet*)touches
- {
- return [self locationFromTouch:[touches anyObject]];
- }
- +(CGPoint) locationFromTouch:(UITouch*)touch
- {
- CGPoint touchLocation = [touch locationInView: [touch view]];
- return [[CCDirector sharedDirector] convertToGL:touchLocation];
- }
两个方法一看就能看出区别,一个是UITouch的,一个是NSSet,一个是单一监听,一个是分发监听;
ok,本章就到这里~~~(下周进入封闭开发了,吃睡都在公司了,咳咳,带上我的多啦a梦的小裤衩,娃哈哈~)
本文转自 xiaominghimi 51CTO博客,原文链接:http://blog.51cto.com/xiaominghimi/632985,如需转载请自行联系原作者