- #include "TouchTest.h"
- TouchTest::TouchTest()
- {
- m_longProgress=false;
- }
- bool isTouch=false;
- bool isMoved=false;
- int pressTimes=0;
- int touchCounts=0;
- void TouchTest::updateSingleDelay(float ft)
- {
- if (touchCounts == 1) {
- onSingleCLick();
- touchCounts=0;
- }
- }
- void TouchTest::updateDoubleDelay(float ft)
- {
- if (touchCounts == 2 )
- {
- onDoubleClick();
- touchCounts=0;
- }
- }
- void TouchTest::updatelongprogress(float ft)
- {
- if (isTouch) {
- pressTimes++;
- if (pressTimes >= 2) {
- m_longProgress=true;
- onLongPressed();
- }
- }
- else
- {
- pressTimes=0;
- }
- }
- long long TouchTest::getCurrentTime()
- {
- struct timeval tm;
- gettimeofday(&tm, NULL);
- return (long long)(tm.tv_sec*1000 + tm.tv_usec/1000);
- }
- bool TouchTest::touchBegan(cocos2d::Touch *touch, cocos2d::Event *event)
- {
- m_startPoint=touch->getLocation();
- isTouch=true;
- m_startTime=getCurrentTime();
- //处理长按事件
- this->schedule(schedule_selector(TouchTest::updatelongprogress),1);
- return true;
- }
- void TouchTest::touchMoved(cocos2d::Touch *touch, cocos2d::Event *event)
- {
- isMoved=true;
- Point curPoint=touch->getLocation();
- onMove(curPoint);
- }
- void TouchTest::touchEnded(cocos2d::Touch *touch, cocos2d::Event *event)
- {
- isTouch=false;
- pressTimes=0;
- this->unschedule(schedule_selector(TouchTest::updatelongprogress));
- //如果刚完成长按事件 则把按下次数清零 长按状态置空 直接返回 不继续执行
- if (m_longProgress ) {
- touchCounts=0;
- m_longProgress=false;
- return;
- }
- m_endPoint=touch->getLocation();
- long long endTime=getCurrentTime();
- long long timeDis=endTime-m_startTime;
- E_SWIP_DIR dir=GetSwipDir(m_startPoint, m_endPoint,timeDis);
- if ( dir != E_INVAILD) {
- onSwip(m_startPoint, m_endPoint, dir);
- return;
- }
- //做连击判断
- if (isMoved) {
- isMoved=false;
- return;
- }
- if (touchCounts == 2) {
- onThreeClick();
- touchCounts=0;
- }
- else if (touchCounts == 1) {
- this->scheduleOnce(schedule_selector(TouchTest::updateDoubleDelay), 0.25);
- touchCounts++;
- }
- else if (touchCounts == 0) {
- this->scheduleOnce(schedule_selector(TouchTest::updateSingleDelay), 0.25);
- touchCounts++;
- }
- }
- #include <iostream>
- #include "cocos2d.h"
- USING_NS_CC;
- const int minSwipdistance=100;
- const int minSwiptime=1000; //毫秒
- const int maxClickedDis=20;
- enum E_SWIP_DIR
- {
- E_INVAILD,
- E_LEFT,
- E_RIGHT,
- E_UP,
- E_DOWN
- };
- class TouchTest:public Layer
- {
- public:
- TouchTest();
- bool touchBegan(Touch * touch,Event * event);
- void touchMoved(Touch * touch,Event * event);
- void touchEnded(Touch * touch,Event * event);
- void touchCancel(Touch * touch,Event * event);
- void onTouchesBegan(const std::vector<Touch*>& touches, Event *unused_event);
- void onTouchesMoved(const std::vector<Touch*>& touches, Event *unused_event);
- void onTouchesEnded(const std::vector<Touch*>& touches, Event *unused_event);
- void onTouchesCancelled(const std::vector<Touch*>&touches, Event *unused_event);
- void updateSingleDelay(float);
- void updateDoubleDelay(float);
- void updatelongprogress(float);
- long long getCurrentTime();
- void stopSchedule();
- private:
- E_SWIP_DIR GetSwipDir(Point start,Point end,long long timeDis);
- public:
- virtual void onSingleCLick(); //单击
- virtual void onDoubleClick(); //双击
- virtual void onThreeClick(); //3连击
- virtual void onLongPressed(); //长按
- virtual void onMove(Point); //移动
- virtual void onSwip(Point,Point,E_SWIP_DIR); //滑动