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 ...
 
随机推荐
- window.showModalDialog的基本用法
			
window.showModalDialog的基本用法 showModalDialog() (IE 4+ 支持) showModelessDialog() (IE 5+ 支持) window.show ...
 - HTML5 Placeholder实现input背景文字提示效果
			
这篇文章我们来看看什么是input输入框背景文字提示效果,如下图所示: 这种效果现在网上非常的普遍流行,但大部分是使用JavaScript实现的.但HTML5给我们提供了新的纯HTML的实现方式,不需 ...
 - 利用VHDL读写file文件
			
library ieee; use std.textio.all; use ieee.std_logic_textio.all; use ieee.std_logic_1164 ...
 - OSG中的视角 eye up center
			
这三个值都是vec3变量,其中eye和center确定视角 eye就相当于人的眼睛,我们观察场景,是从这个坐标去看的,然后有了眼睛,我们观察得有一个方向,那么久需要另外一个坐标,就是c ...
 - Linux中的cron计划任务配置方法(详细)
			
cron来源于希腊单词chronos(意为“时间”),指Linux系统下一个自动执行指定任务的程序(计划任务) 1. crontab命令选项 #crontab -u <-l, -r, -e> ...
 - Oracle EBS-SQL (SYS-18):检查系统安装的各个表是否打开(PJM%).sql
			
select status, trigger_name from all_triggers where trigger_name like 'PJM%'; ALTER TRIGGER PJM_ORG_ ...
 - Nand ECC校验和纠错原理及2.6.27内核ECC代码分析
			
ECC的全称是Error Checking and Correction,是一种用于Nand的差错检测和修正算法.如果操作时序和电路稳定性不存在问题的话,NAND Flash出错的时候一般不会造成整个 ...
 - Android 屏幕适配方式
			
适配:即当前应用在相同的手机上面显示相同的效果.适配前需要首先确定当前手机所属像素密度类型(如:xhdpi.hdpi.mdpi等) 像素密度:每英寸上分布的像素点个数,单位(dpi,ppi),利用勾股 ...
 - 使用session插件并且实现登录验证
			
var express = require('express'); var cookieParser = require('cookie-parser'); var bodyParser = requ ...
 - JavaSE思维导图(四)