手勢識別器

2021-07-07 06:36:40 字數 3341 閱讀 7899

注意要把物件imageview的互動開了

// 建立乙個imageview 新增手勢用

uiimage *image = [uiimage imagenamed:@"selected"];

uiimageview *imageview = [[uiimageview alloc] initwithframe:[uiscreen mainscreen].bounds];

imageview.image = image;

imageview.userinteractionenabled = yes;

[self

.view addsubview:imageview];

[imageview release];

建立好imageview後 把建立的手勢物件新增到imageview上去:

手勢類 uigesturerecognizer

這個類是個抽象類 其具體功能 交給子類去實現

抽象類就是省**的類

// 初始化乙個輕拍手勢的物件

uitapgesturerecognizer *tap = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(tapaction:)];

// 新增手勢到檢視上

[imageview addgesturerecognizer:tap];

// 釋放

[tap release];

// 實現輕拍方法

- (void)tapaction:(uitapgesturerecognizer *)tap

// 長按

// 新增手勢步驟

// 1. 初始化手勢 新增手勢觸發呼叫的方法

// 2. 把手勢新增到檢視上

// 3. 釋放手勢

uilongpressgesturerecognizer *longpress = [[uilongpressgesturerecognizer alloc] initwithtarget:self action:@selector(longpressaction:)];

// 設定長按時間

longpress.minimumpressduration = 2.0;

[imageview addgesturerecognizer:longpress];

[longpress release];

// 實現長按手勢

- (void)longpressaction:(uilongpressgesturerecognizer *)longpress

}

uirotationgesturerecognizer *rotation = [[uirotationgesturerecognizer alloc] initwithtarget:

self

action:

@selector(rotationaciton:)];

[imageview addgesturerecognizer:rotation];

[rotation release];

// 實現旋轉手勢

- (void)rotationaciton:(uirotationgesturerecognizer *)rotation

uipinchgesturerecognizer *pinch = [[uipinchgesturerecognizer alloc] initwithtarget:

self

action:

@selector(pinchaciton:)];

[imageview addgesturerecognizer:pinch];

[pinch release];

// 實現捏合收拾方法

- (void)pinchaciton:(uipinchgesturerecognizer *)pinch

uipangesturerecognizer *pan = [[uipangesturerecognizer alloc] initwithtarget:

self

action:

@selector(panaction:)];

[imageview addgesturerecognizer:pan];

[pan release];

// 實現平移方法

- (void)panaction:(uipangesturerecognizer *)pan

uiswipegesturerecognizer *swipe = [[uiswipegesturerecognizer alloc] initwithtarget:

self

action:

@selector(swipeaction:)];

swipe.direction = uiswipegesturerecognizerdirectionleft;

[imageview addgesturerecognizer:swipe];

[swipe release];

// 實現輕掃方法

- (void)swipeaction:(uiswipegesturerecognizer *)swipe

這個類繼承於uipangesturerecognizer

uiscreenedgepangesturerecognizer *screenedgepan = [[uiscreenedgepangesturerecognizer alloc] initwithtarget:

self

action:

@selector(screenedgepanaction:)];

// 設定下從哪個邊緣開始掃

screenedgepan.edges = uirectedgeright;

[imageview addgesturerecognizer:screenedgepan];

[screenedgepan release];

// 實現邊緣掃

- (void)screenedgepanaction:(uiscreenedgepangesturerecognizer *)screenedgepan

手勢識別器(敲擊

一 監聽觸控事件的做法 如果想監聽乙個view上面的觸控事件,之前的做法通常是 先自定義乙個view,然後再實現view的touches方法,在方法內部實現具體處理 通過touches方法監聽view觸控事件,有很明顯的幾個缺點 1 必須得自定義view 2 由於是在view內部的touches方法...

Android 手勢識別器

手勢識別器 1,定義出來 2,例項化 把想要的方法給重寫 3,在ontouchevent 把事件傳遞給手勢識別器private gesturedetector detector 將detector例項化 裡面的方法可以ctrl o,看裡面有什麼方法 private void initview fin...

iOS 手勢識別器概述

其相應的方法 initwithtarget action addtarget action removetarget action locationinview locationoftouch inview numberoftouches屬性 requiregesturerecognizertofa...