手勢識別器(敲擊

2021-06-28 20:36:02 字數 1827 閱讀 1178

一、監聽觸控事件的做法

如果想監聽乙個view上面的觸控事件,之前的做法通常是:先自定義乙個view,然後再實現view的touches方法,在方法內部實現具體處理**

通過touches方法監聽view觸控事件,有很明顯的幾個缺點

(1)必須得自定義view

(2)由於是在view內部的touches方法中監聽觸控事件,因此預設情況下,無法讓其他外界物件監聽view的觸控事件(需要通過**)

(3)不容易區分使用者的具體手勢行為

ios 3.2之後,蘋果推出了手勢識別功能(gesture recognizer),在觸控事件處理方面,大大簡化了開發者的開發難度

二、手勢識別器

為了完成手勢識別,必須借助於手勢識別器----uigesturerecognizer

利用uigesturerecognizer,能輕鬆識別使用者在某個view上面做的一些常見手勢

uigesturerecognizer是乙個抽象類,定義了所有手勢的基本行為,使用它的子類才能處理具體的手勢

uitapgesturerecognizer(敲擊)

uipinchgesturerecognizer(捏合,用於縮放)

uipangesturerecognizer(拖拽)

uiswipegesturerecognizer(輕掃)

uirotationgesturerecognizer(旋轉)

uilongpressgesturerecognizer(長按)

三、敲擊

每乙個手勢識別器的用法都差不多,比如uitapgesturerecognizer的使用步驟如下:

(1)建立手勢識別器物件

uitapgesturerecognizer *tap = [[uitapgesturerecognizer alloc] init];

(2)設定手勢識別器物件的具體屬性

// 連續敲擊2次

tap.numberoftapsrequired = 2;

// 需要2根手指一起敲擊

tap.numberoftouchesrequired = 2;

(3)新增手勢識別器到對應的view上

[self.iconview addgesturerecognizer:tap];

(4)監聽手勢的觸發

[tap addtarget:self action:@selector(tapiconview:)];

屬性介紹:

numberoftouchesrequired   //需要多少根手指一起敲擊(預設為1根)

numberoftapsrequired    //需要敲擊多少下(預設為1)

四、實現**

1//2

02-手勢識別器(敲擊)4//

5//6//

7//89

#import

"yyviewcontroller.h"10

11@inte***ce

yyviewcontroller ()

12 @property (weak, nonatomic) iboutlet uiimageview *iconview;

1314

@end

1516

@implementation

yyviewcontroller

1718 - (void

)viewdidload

1937 -(void

)tapview

3841

4243

@end

手勢識別器

注意要把物件imageview的互動開了 建立乙個imageview 新增手勢用 uiimage image uiimage imagenamed selected uiimageview imageview uiimageview alloc initwithframe uiscreen main...

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...