在onTouchesBegan中,touches.size()一直是1。这对多点缩放图片,三维模型来说,挺郁闷的。本文把初始值放在onTouchesMoved 中,基本解决问题,但有一小点问题,就是第一次触摸时有小范围的缩放,因为只有移动才可以得出新的距离,但是此时的距离一定不等于之前的距离。对我的项目够了,不知道其它人能不能用。
void CocaineLayer::onTouchesMoved(const std::vector<Touch*>& touches,cocos2d::Event *event) { if (touches.size() >= 2) { if (mulTouchesFlag) { auto t0 = touches[0]; auto t1 = touches[1]; auto location0 = t0->getLocation(); auto location1 = t1->getLocation(); _oldDis = (float) sqrt((location0.x - location1.x) * (location0.x - location1.x) + (location0.y - location1.y) * (location0.y - location1.y)); mulTouchesFlag = false; } else { auto t0 = touches[0]; auto t1 = touches[1]; auto location0 = t0->getLocation(); auto location1 = t1->getLocation(); float newDis = (float) sqrt((location0.x - location1.x) * (location0.x - location1.x) + (location0.y - location1.y) * (location0.y - location1.y)); _scale *= newDis / _oldDis; _model->setScale(_scale * 0.01f); if (_scale > 10.0f) { _scale = 10.0f; } if (_scale < 0.7f) { _scale = 0.7f; } _oldDis = newDis; } } }
void CocaineLayer::onTouchesEnded(const std::vector<Touch*>& touches,cocos2d::Event *event) { mulTouchesFlag = true; }仅供参考