首先,来看下代码:

声明文件:

#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 多点触控实现缩放及相关问题的解决方法的更多相关文章

  1. android实现图片平铺效果&WebView多点触控实现缩放

    1.图片平铺效果实现非常简单,只要在xml中添加一个 android:tileMode的属性就可以了.首先在drawable文件夹中添加自己的my.xml文件.代码: Java代码 <?xml ...

  2. Android多点触控技术实战,自由地对图片进行缩放和移动

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/11100327 在上一篇文章中我带着大家一起实现了Android瀑布流照片墙的效果, ...

  3. windows8 开发教程 教你制作 多点触控Helper可将任意容器内任意对象进行多点缩放

    http://blog.csdn.net/wangrenzhu2011/article/details/7732907 (转) 实现方法: 对Manipulation进行抽象化 使不同容器可共用多点缩 ...

  4. Android多点触控(图片的缩放Demo)

    本文主要介绍Android的多点触控,使用了一个图片缩放的实例,来更好的说明其原理.须要实现OnTouchListener接口,重写当中的onTouch方法. 实现效果图:       源码: 布局文 ...

  5. (干货) Android实现ImageVIew多点触控及双击缩放

    支持多点触控,放大自由移动,双击可以放大缩小.直接上代码: package com.cbt.view; import android.content.Context; import android.g ...

  6. (一)自定义ImageView,初步实现多点触控、自由缩放

    真心佩服那些一直专注于技术共享的大神们,正是因为他们无私的分享精神,我才能每天都有进步.近日又算是仔细学了android的自定义控件技术,跟着大神的脚步实现了一个自定义的ImageView.里面涉及到 ...

  7. unity3d 触屏多点触控(旋转与缩放)

    unity3d 触屏多点触控(旋转与缩放) /*Touch OrbitProgrammed by: Randal J. Phillips (Caliber Mengsk)Original Creati ...

  8. 【原】cocos2d-x开发笔记:多点触控

    在项目开发中,我们做的大地图,一个手指头按下滑动可以拖动大地图,两个手指头按下张开或者闭合,可以放大和缩小地图 在实现这个功能的时候,需要使用到cocos2d-x的多点触控功能. 多点触控事件,并不是 ...

  9. Cocos2dx 多点触控

    1 最容易忽略的东西,对于ios平台,须得设置glView的属性: [__glView setMultipleTouchEnabled:YES]; 2 如果调用CCLayer的方法setTouchEn ...

随机推荐

  1. cad移动图案

    1.键盘按快捷键M,鼠标变成小方块,选中图案,右击,左击拖动图像 2.选中图像,键盘按m,拖动图像 3.选中图像,右击,图像移动

  2. WEKA,一个开源java的数据挖掘工具

    开始研究WEKA,一个开源java的数据挖掘工具. HS沉寂这么多天,谁知道偏偏在我申请离职的时候给我安排了个任务,哎,无语. 于是,今天看了一天的Weka. 主要是看了HS提供的三个文章(E文,在g ...

  3. 剑指offer 22 栈的压入、弹出序列

    class Solution { public: bool IsPopOrder(vector<int> pushV,vector<int> popV) { bool resu ...

  4. ExecuteScalar

    ExecuteScalar运行查询,并返回查询所返回的结果集中第一行的第一列或空引用(假设结果集为空).忽略其它列或行. 使用 ExecuteScalar 方法从数据库中检索单个值. 由于不用创建行集 ...

  5. 游戏开场镜头拉近(Unity3D开发之四)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢! 原文地址: http://www.cocos2dev.com/? p=553 今天看了个Demo.发现 ...

  6. xcode UILabel创建和隐藏

    // 创建label UILabel *label = [[UILabel alloc] init]; // 设置显示的文字 label.text = @"Hello world!Hello ...

  7. 一键搞定Java桌面应用安装部署 —— exe4j + Inno Setup 带着JRE, 8M起飞

    转载自:http://www.blogjava.net/huliqing/archive/2008/04/18/193907.html 对于作Java桌面应用来说,比较烦人的就是安装部署问题,客户端是 ...

  8. decimal类型不能为空,自定义update更新null值的问题。

    if (!string.IsNullOrEmpty(yt_time_limit_1)) { entity["yt_time_limit_1"] = Convert.ToDecima ...

  9. sql中复合组建解析

    每个表只能有一个主键 唯一键/约束可以多个 复合主键是指复合唯一,比如: 1 1 1 2 2 1 2 2 单看任何一列都不是唯一,但组合起来就是唯一的. Create Table 表名 (字段名1 I ...

  10. Spring学习之常用注解(转)

    使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册<context:component-scan base-package ...