原理:
1): 判断点击时是单指还是双指,然后再move中进行移动放大和缩小的操作
注意: ccTouchesBegan()和ccTouchesMoved()函数,它的意思是点击几次,就有几次ccTouchesBegan事件,并且当你点击,但没有进行移动时,也会响应ccTouchesMoved事件,你们可以试试看。
2): 放大和缩小时的地图的锚点的设置: 首先记录下地图的contentSize,还有初始原点位置,还有锚点位置。
实现:
HelloWorldScene.h
//点击事件 virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent); virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent); virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent); //两只手指点击时的初始距离 float distance; //背景图 CCSprite* sBg; //放大的倍数 float m_scale; //记录原点位置 CCPoint bgOrigin; //存touch事件 CCDictionary* _touchesDic;
HelloWorldScene.cpp
init():
//初始化背景 sBg = CCSprite::create("bg.jpg"); sBg->setAnchorPoint(CCPointZero); sBg->setPosition( CCPointZero ); this->addChild(sBg, 0); //原点位置 bgOrigin = CCPointZero; //初始化数据 m_scale = 1.0f; distance = 0.0f; _touchesDic = CCDictionary::create(); _touchesDic->retain(); //设置可以点击 this->setTouchEnabled(true);
CCSetIterator iter = pTouches->begin(); for (; iter != pTouches->end(); iter++){ CCTouch* pTouch = (CCTouch*)(*iter); _touchesDic->setObject(pTouch, CCString::createWithFormat("%d",pTouch->getID())->getCString()); } //两个手指 if (_touchesDic->count() == 2){ CCArray* keys = _touchesDic->allKeys(); CCTouch *touch1 = (CCTouch*)_touchesDic->objectForKey(((CCString*)keys->objectAtIndex(0))->getCString()); CCTouch *touch2 = (CCTouch*)_touchesDic->objectForKey(((CCString*)keys->objectAtIndex(1))->getCString()); CCPoint pt = touch1->getLocation(); CCPoint pt2 = touch2->getLocation(); distance = ccpDistance(pt,pt2); }
if (_touchesDic->count() == 2) { CCArray* keys = _touchesDic->allKeys(); CCTouch *touch1 = (CCTouch*)_touchesDic->objectForKey(((CCString*)keys->objectAtIndex(0))->getCString()); CCTouch *touch2 = (CCTouch*)_touchesDic->objectForKey(((CCString*)keys->objectAtIndex(1))->getCString()); CCPoint point1 = touch1->getLocation(); CCPoint point2 = touch2->getLocation(); //点击点距原点距离 CCPoint mPoint1 = ccpSub(point1,bgOrigin); CCPoint mPoint2 = ccpSub(point2,bgOrigin); //点击点的中心点 float x = (mPoint2.x+mPoint1.x)/2 ; float y = (mPoint2.y+mPoint1.y)/2 ; //得到锚点值 CCSize bgSize = sBg->getContentSize(); float anchorX = x/(bgSize.width*sBg->getScale()); float anchorY = y/(bgSize.height*sBg->getScale()); CCLOG("bg anchor: %lf,%lf",anchorX,anchorY); sBg->setAnchorPoint(ccp(anchorX,anchorY)); //位置 x = (point2.x+point1.x)/2 ; y = (point2.y+point1.y)/2 ; sBg->setPosition(ccp(x,y)); //放大的倍率 float mdistance = ccpDistance(mPoint1,mPoint2); m_scale = (mdistance/distance)*m_scale; if (m_scale>=3.0)//放大缩小的最值 { sBg->setScale(3.0f); } else if (m_scale<=1.0) { sBg->setScale(1.0f); } else { distance = mdistance; sBg->setScale(m_scale); } m_scale = sBg->getScale(); //原点值 bgOrigin = ccpSub(ccp(x,y),ccp(bgSize.width*sBg->getScale()*anchorX,bgSize.height*sBg->getScale()*anchorY)); } else if (_touchesDic->count() == 1)//单指 { CCSetIterator iter = pTouches->begin(); CCPoint pDelta = ((CCTouch*)(*iter))->getDelta(); CCPoint postion = sBg->getPosition(); sBg->setPosition(ccpAdd(postion, pDelta)); //原点位置 bgOrigin = ccpAdd(bgOrigin,pDelta); } else { }
//恢复原值
if (_touchesDic->count() == 2) { distance = 0.0f; } else { } CCSetIterator iter = pTouches->begin(); for (; iter != pTouches->end(); iter++){ CCTouch* pTouch = (CCTouch*)(*iter); _touchesDic->removeObjectForKey(CCString::createWithFormat("%d",pTouch->getID())->getCString()); }
这样差不多就可以实现单指拖动,双指放大和缩小
该项目代码下载(Android版本)地址:
http://download.csdn.net/detail/cwn0812/6642203