簡單手勢識別

2022-03-11 22:19:23 字數 3283 閱讀 9352

模擬ios原生手勢,簡單實現點選(雙擊)、長按、滑動、拖動等功能。

**如下:

//

// cgesture.h

// actionlabel

//// created by xujw on 16/3/15.

/* 手勢識別 仿iphone 簡單長按 點選(雙擊等) 滑動 拖動等。

* 使用方法:

* auto gesture = cgesture::createtapgesture(target,callback,taps);

* this->addchild(gestrue)

*/#ifndef cgesture_h

#define cgesture_h

#include

#include "cocos2d.h"

using_ns_cc;

typedef

enum gesturedirection

gesturedirection;

typedef

std::function gesturecallback;

typedef

std::function swipegesturecallback;

#define shake_length 100

class cgesture:public layer

;#endif /* cgesture_h */

//// cgesture.cpp

// actionlabel

//// created by xujw on 16/3/15.

////

#include "cgesture.h"

#include

cgesture::cgesture()

:_iscantouch(false)

,_tapnum(0)

{}cgesture::~cgesture()

{}bool cgesture::init()

return

true;

}cgesture* cgesture::createtapgesture(node*target,gesturecallback callback,int tapsrequired)

if (box.containspoint(pos))

return

true;

};lis->ontouchmoved = [=](touch *touch,event *event)

};lis->ontouchended = [=](touch *touch,event *event)

if (box.containspoint(pos))

}gesture->scheduleonce([=](float dt)

, 0.5, "gesturetapnumreset");

};gesture->geteventdispatcher()->addeventlistenerwithscenegraphpriority(lis, target);

return gesture;

}cgesture* cgesture::createlongpressgesture(node*target,gesturecallback callback,float delay)

if (box.containspoint(pos))

, delay, "gesturechangetouchstate");

return

true;

}else

};lis->ontouchmoved = [=](touch *touch,event *event)

};lis->ontouchended = [=](touch *touch,event *event)

if (gesture->_iscantouch)

}else

gesture->_iscantouch = false;

};gesture->geteventdispatcher()->addeventlistenerwithscenegraphpriority(lis, target);

return gesture;

}cgesture* cgesture::createswipegesture(node*target,swipegesturecallback callback)

if (box.containspoint(pos))

else

return gesture->_iscantouch;

};lis->ontouchmoved = [=](touch *touch,event *event){};

lis->ontouchended = [=](touch *touch,event *event)

//起始點都在target範圍內才響應

if (!gesture->_iscantouch || !box.containspoint(pos))

auto startpos = touch->getstartlocation();

auto curpos = touch->getlocation();

auto subpos = curpos - startpos;

if (fabs(subpos.x) > fabs(subpos.y))

else

if (subpos.x < -shake_length)

}else

else

if (subpos.y < -shake_length)}};

gesture->geteventdispatcher()->addeventlistenerwithscenegraphpriority(lis, gesture);

return gesture;

}cgesture* cgesture::createpangestrue(node*target,gesturecallback callback)

if (box.containspoint(pos))

return

true;

};lis->ontouchmoved = [=](touch *touch,event *event)

};lis->ontouchended = [=](touch *touch,event *event)

;gesture->geteventdispatcher()->addeventlistenerwithscenegraphpriority(lis, target);

return gesture;

}

iPhone 簡單手勢的判斷

不知道4.0sdk帶有手勢的直接支援沒有,至少3.2已經可以用了.但是如果想支援早期的版本,那麼手勢的識別無疑是一種痛苦,因為需要自己寫 來判定手勢.下面 是判斷乙個滑動的手勢 swipe 雖然很簡單但是總體思想就是這樣了.當在乙個水平,或者縱向滑動時給出乙個滑動距離以及偏移量.當實際滑動距離超過指...

opencv實現簡單手指位置識別

整體思路 1.過濾雜訊 2.由於rgb顏色的離散性轉換為hsv通道 3.對hsv空間進行量化,得到2值影象,亮的部分為手的形狀 4.去除雜點造成的偽輪廓,留下手的真實輪廓 5.對凸出點連線 6.最高點到底部中點的連線即為手指方向 部分 將mfc實現部分給省略了,給出了完整的opencv部分 可以參考...

swift 手勢識別

viewcontroller.swift jieuitapgesturerecognizer created by jiezhang on 14 10 4.import uikit class viewcontroller uiviewcontroller,uiactionsheetdelegate...