cocos2d-x 多点触控实现缩放及相关问题的解决方法
首先,来看下代码:
声明文件:
#ifndef __loading__MoreTouches__
#define __loading__MoreTouches__ #include <iostream>
#include "cocos2d.h"
USING_NS_CC;
class MoreTouches :public CCLayer
{
public:
bool init();
//virtual void registerWithTouchDispather(void); //由于是继承自CCLayer,这个方法就不用重写了,但下面几个方法还是要重写滴
virtual void ccTouchesCancellnd(CCSet *pTouches,CCEvent *pEvent);
virtual void ccTouchesBegan(CCSet *pTouches,CCEvent *pEvent);//注意这个方法和单点触控方法的返回类型不同
virtual void ccTouchesEnded(CCSet *pTouches,CCEvent *pEvent);
virtual void ccTouchesMoved(CCSet *pTouches,CCEvent *pEvent);
static CCScene *scene();
virtual void onEnter();
virtual void onExit();
CREATE_FUNC(MoreTouches); public:
double distance; //两个触摸点之间的距离
double deltax; //目标x轴的改变值
double deltay; //目标y轴的改变值
CCSprite *bg; //目标精灵
double mscale; //初始地图缩放比例 }; #endif /* defined(__loading__MoreTouches__) */
定义文件:
#include "MoreTouches.h" bool MoreTouches::init()
{
if(!CCLayer::init())
{
return false;
}
bg=CCSprite::create("fullbg.png"); //初始化目标图片
this->addChild(bg); mscale=1.0; //初始化图片的缩放比例
return true;
} //void MoreTouches::registerWithTouchDispather()
//{
// CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this, 0);
//} void MoreTouches::ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent)
{
if(pTouches->count()>=2) //如果触摸点不少于两个
{
CCSetIterator iter=pTouches->begin();
CCPoint mPoint1=((CCTouch *)(*iter))->getLocationInView();
mPoint1 = CCDirector::sharedDirector()->convertToGL(mPoint1);
iter++;
CCPoint mPoint2=((CCTouch *)(*iter))->getLocationInView();
mPoint2 = CCDirector::sharedDirector()->convertToGL(mPoint2); distance=sqrt((mPoint2.x-mPoint1.x)*(mPoint2.x-mPoint1.x)+(mPoint2.y-mPoint1.y)*(mPoint2.y-mPoint1.y));//计算两个触摸点距离
deltax = (mPoint1.x + mPoint2.x)/2 - bg->getPositionX(); //得到两个触摸点中点和精灵锚点的差值
deltay = (mPoint1.y + mPoint2.y)/2 - bg->getPositionY();
CCLog("ccTouchesBegan ..."); }
}
void MoreTouches::ccTouchesMoved(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent)
{
if(pTouches->count()>=2) //如果移动时触摸点的个数不少于两个
{
CCSetIterator iter = pTouches->begin();
CCPoint mPoint1 = ((CCTouch*)(*iter))->getLocationInView();
mPoint1 = CCDirector::sharedDirector()->convertToGL(mPoint1);
iter++;
CCPoint mPoint2 = ((CCTouch*)(*iter))->getLocationInView();
mPoint2 = CCDirector::sharedDirector()->convertToGL(mPoint2); //获得新触摸点两点之间的距离
double mdistance = sqrt((mPoint1.x-mPoint2.x)*(mPoint1.x-mPoint2.x)+(mPoint1.y-mPoint2.y)*(mPoint1.y-mPoint2.y));
mscale = mdistance/distance * mscale; // 新的距离 / 老的距离 * 原来的缩放比例,即为新的缩放比例
distance = mdistance;
bg->setScale(mscale); double x = (mPoint2.x+mPoint1.x)/2 - deltax; //计算两触点中点与精灵锚点的差值
double y = (mPoint2.y+mPoint1.y)/2 - deltay;
bg->setPosition(ccp(x,y)); //保持两触点中点与精灵锚点的差值不变
deltax = (mPoint1.x+ mPoint2.x)/2 - bg->getPositionX(); //计算新的偏移量
deltay = (mPoint2.y + mPoint1.y)/2 - bg->getPositionY();
CCLog("ccTouchMoved ....");
}
if(pTouches->count()==1) //如果触摸点为一个
{
CCSetIterator iter = pTouches->begin();
CCPoint mPoint=((CCTouch*)(*iter))->getLocationInView();
mPoint=CCDirector::sharedDirector()->convertToGL(mPoint); //坐标转换
bg->setPosition(mPoint); //直接移动精灵
}
}
void MoreTouches::ccTouchesEnded(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent)
{ }
void MoreTouches::ccTouchesCancellnd(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent)
{ }
CCScene *MoreTouches::scene()
{
CCScene *scene=CCScene::create();
MoreTouches *layer=MoreTouches::create();
scene->addChild(layer);
return scene;
}
void MoreTouches::onEnter()
{
CCLayer::onEnter();
setTouchEnabled(true);CCLog("onenter");
}
void MoreTouches::onExit()
{
CCLayer::onExit();
CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this); //移除触摸代理
}
首先要实现缩放这个逻辑,在ccTouchesBegan中检测,如果触摸点的个数大于两个,那么取前两个点,使用两点距离公式计算两点距离,在ccTouchesMoved中检测,如果触摸点的个数大于两个,那么继续计算这两个点的距离,然后通过距离,计算得到缩放比例,调用setScale设置缩放比例即可。
另外,除了缩放处理外,还需要处理位置问题,在ccTouchesBegan中计算两个触点的中点位置和精灵锚点的差,在ccTouchesMoved中,随着缩放,保持两触点中点和精灵锚点的差不变即可,ccTouchesEnded和ccTouchesCancelled中不需要修改。
接下来,就重点说下在实现时遇到的问题吧,主要就一个,就是当在ios模拟器中操作时,我按着option键,可以出现两个点,但程序中始终只能得到一个点,即count=1,
移动时也根本不能实现缩放。后来,终于谷歌出结果了,原来需要修改ios目录下的 AppController.mm文件
static AppDelegate s_sharedApplication;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
EAGLView *__glView = [EAGLView viewWithFrame: [window bounds]
pixelFormat: kEAGLColorFormatRGBA8
depthFormat: GL_DEPTH_COMPONENT16
preserveBackbuffer: NO
sharegroup: nil
multiSampling: NO
numberOfSamples:0 ]; // Use RootViewController manage EAGLView
viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
viewController.wantsFullScreenLayout = YES;
viewController.view = __glView; // Set RootViewController to window
if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)
{
// warning: addSubView doesn't work on iOS6
[window addSubview: viewController.view];
}
else
{
// use this method on ios6
[window setRootViewController:viewController];
}
[window makeKeyAndVisible];
[[UIApplication sharedApplication] setStatusBarHidden: YES];
cocos2d::CCApplication::sharedApplication()->run();
[__glView setMultipleTouchEnabled:YES]; //主要就是这句了
return YES;
}
简单来说,就是: AppController.mm 里要启用多点触摸才可以,在- (BOOL)application:(UIApplication *)application添加[__glView setMultipleTouchEnabled:YES];
这样,当我们按着option键和鼠标时,移动鼠标,就可以实现缩放了,当只用鼠标时,可以实现图片的移动,问题解决。
cocos2d-x 多点触控实现缩放及相关问题的解决方法的更多相关文章
- android实现图片平铺效果&WebView多点触控实现缩放
1.图片平铺效果实现非常简单,只要在xml中添加一个 android:tileMode的属性就可以了.首先在drawable文件夹中添加自己的my.xml文件.代码: Java代码 <?xml ...
- Android多点触控技术实战,自由地对图片进行缩放和移动
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/11100327 在上一篇文章中我带着大家一起实现了Android瀑布流照片墙的效果, ...
- windows8 开发教程 教你制作 多点触控Helper可将任意容器内任意对象进行多点缩放
http://blog.csdn.net/wangrenzhu2011/article/details/7732907 (转) 实现方法: 对Manipulation进行抽象化 使不同容器可共用多点缩 ...
- Android多点触控(图片的缩放Demo)
本文主要介绍Android的多点触控,使用了一个图片缩放的实例,来更好的说明其原理.须要实现OnTouchListener接口,重写当中的onTouch方法. 实现效果图: 源码: 布局文 ...
- (干货) Android实现ImageVIew多点触控及双击缩放
支持多点触控,放大自由移动,双击可以放大缩小.直接上代码: package com.cbt.view; import android.content.Context; import android.g ...
- (一)自定义ImageView,初步实现多点触控、自由缩放
真心佩服那些一直专注于技术共享的大神们,正是因为他们无私的分享精神,我才能每天都有进步.近日又算是仔细学了android的自定义控件技术,跟着大神的脚步实现了一个自定义的ImageView.里面涉及到 ...
- unity3d 触屏多点触控(旋转与缩放)
unity3d 触屏多点触控(旋转与缩放) /*Touch OrbitProgrammed by: Randal J. Phillips (Caliber Mengsk)Original Creati ...
- 【原】cocos2d-x开发笔记:多点触控
在项目开发中,我们做的大地图,一个手指头按下滑动可以拖动大地图,两个手指头按下张开或者闭合,可以放大和缩小地图 在实现这个功能的时候,需要使用到cocos2d-x的多点触控功能. 多点触控事件,并不是 ...
- Cocos2dx 多点触控
1 最容易忽略的东西,对于ios平台,须得设置glView的属性: [__glView setMultipleTouchEnabled:YES]; 2 如果调用CCLayer的方法setTouchEn ...
随机推荐
- css3之border-color
-moz-border-top-colors:上边框-moz-border-right-colors:右边框-moz-border-bottom-colors:下边框-moz-border-left- ...
- tail -f
http://stackoverflow.com/questions/187587/looking-for-a-windows-equivalent-of-the-unix-tail-command ...
- Xcode 3.2.5免证书开发调试
Xcode 3.2.5免证书开发调试 xcode3.2.5 应该没人用了.这里做个保存而已. Xcode编译遇到过 Code Sign error: a valid provisioning prof ...
- poj1862---变形虫(贪心)
题意:两条虫之间碰在一起,质量变为2*sqrt(m1*m2) 求怎么结合,能使最后的一只虫质量最小 分析:如果让按从大到小的顺序依次结合,可以使大的数被开方的次数最多,得到的结果更小 4 3 2 1 ...
- [置顶] Oracle GoldenGate 常见问题:长事务处理
长事务的影响 OGG是基于事务级的实时复制工具,也就是说OGG只复制已提交的事务,在遇到事务的commit或rollback之前,它会将每个事务的操作存储在称为cache的托管虚拟内存池中.内存再大也 ...
- Data visualization 课程 笔记3
Learn how humans work to create a more effective computer interface 三种reasoning的方式 Deductive Reason ...
- Cookie、Session
Cookie.Session Cookie 保存在浏览器端. 4kb 只能保存字符串,还不能是中文. 获取:Request.getCookies(); 设置时间:setMaxAge(); 小于零是浏览 ...
- windows 7 memcached报failed to install service or service already installed的解决方案
今天心血来潮捣鼓一下memcache,由于系统是windows 7,我参考了 Windows下安装Memcache 使用memcached for Win32. 在运行memcached.exe -d ...
- struts2中从数据库中读取数据,并在JSP页面中遍历保存有JavaBean对象的List对象
0x1:前言 前面搭建struts就不说了,大家都能成功完毕. 0x2:Model 这里我们须要一个Model类来接收 <span style="font-size:10px;font ...
- ZOJ 3829 Known Notation (2014牡丹江H称号)
主题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=5383 Known Notation Time Limit: 2 S ...