若是想監聽一個view上面的觸摸事件,以前的作法一般是:先自定義一個view,而後再實現view的touches方法,在方法內部實現具體處理代碼ui
經過touches方法監聽view觸摸事件,有很明顯的幾個缺點spa
(1)必須得自定義view3d
(2)因爲是在view內部的touches方法中監聽觸摸事件,所以默認狀況下,沒法讓其餘外界對象監聽view的觸摸事件(須要經過代理)代理
(3)不容易區分用戶的具體手勢行爲對象
iOS 3.2以後,蘋果推出了手勢識別功能(Gesture Recognizer),在觸摸事件處理方面,大大簡化了開發者的開發難度事件
2、手勢識別器ip
爲了完成手勢識別,必須藉助於手勢識別器----UIGestureRecognizer開發
利用UIGestureRecognizer,能輕鬆識別用戶在某個view上面作的一些常見手勢get
UIGestureRecognizer是一個抽象類,定義了全部手勢的基本行爲,使用它的子類才能處理具體的手勢it
UITapGestureRecognizer(敲擊)
UIPinchGestureRecognizer(捏合,用於縮放)
UIPanGestureRecognizer(拖拽)
UISwipeGestureRecognizer(輕掃)
UIRotationGestureRecognizer(旋轉)
UILongPressGestureRecognizer(長按)
// 連續敲擊2次
tap.numberOfTapsRequired = 2;
// 須要2根手指一塊兒敲擊
tap.numberOfTouchesRequired = 2;
屬性介紹:
numberOfTouchesRequired //須要多少根手指一塊兒敲擊(默認爲1根)
numberOfTapsRequired //須要敲擊多少下(默認爲1)
提示:swipe.direction=UISwipeGestureRecognizerDirectionDown|UISwipeGestureRecognizerDirectionUp;這種方式最多隻支持1個方向的清掃。
五:UIPanGestureRecognizer
UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]init];
CGPoint point=[pan locationInView:pan.view];
2,返回的值以觸摸點爲原點的距離
[pan setTranslation:CGPointZero inView:pan.view];
CGPoint point1=[pan translationInView:pan.view];