在iOS中UIView是繼承于UIResponder的,而UIResponder是專門用來(lái)響應(yīng)用戶的操作處理各種事件的,包括觸摸事件(Touch Events)、運(yùn)動(dòng)事件(Motion Events)、遠(yuǎn)程控制事件(Remote Control Events,如插入耳機(jī)調(diào)節(jié)音量觸發(fā)的事件),而很多我們常用的類也繼承于UIResponder(UIApplication、UIView、UIViewController).
而以下幾個(gè)方法
@interface UIResponder : NSObject
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;//觸摸屏幕
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;//在屏幕上移動(dòng)
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;//離開屏幕
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
是響應(yīng)觸摸事件的方法,我們可以利用這幾個(gè)方法自定義自己的手勢(shì)。當(dāng)然Apple也為我們提供了幾個(gè)基礎(chǔ)的封裝的手勢(shì)提供使用(了UIGestureRecognizer手勢(shì)識(shí)別)
這里并不深入研究手勢(shì)的響應(yīng)和傳遞,而是研究下幾個(gè)基礎(chǔ)的手勢(shì)和touchs的關(guān)系,這里主要利用這幾個(gè)內(nèi)置的手勢(shì)方法:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapEvent:)];
[self addGestureRecognizer:tap];//點(diǎn)擊
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(tapEvent:)];
[self addGestureRecognizer:pan];//平移,慢速移動(dòng)
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(tapEvent:)];//滑動(dòng),快速移動(dòng)
[self addGestureRecognizer:swipe];
UILongPressGestureRecognizer *longG = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(tapEvent:)];//長(zhǎng)按
[self addGestureRecognizer:longG];

屏幕快照 2018-07-30 上午9.19.35.png
藍(lán)色部分就是需要添加手勢(shì)的view,我們分別添加上述的手勢(shì)進(jìn)行測(cè)試,同時(shí)實(shí)現(xiàn)touchs觸摸相關(guān)方法

屏幕快照 2018-07-30 上午9.39.21.png
首先是UITapGestureRecognizer的方法執(zhí)行順序

屏幕快照 2018-07-30 上午9.21.30.png
很容易理解,因?yàn)橹皇莟ap單擊事件,所以在檢測(cè)到begin touch時(shí)手勢(shì)事件就開始響應(yīng),同時(shí)并不會(huì)有move動(dòng)作
然后是UIPanGestureRecognizer

屏幕快照 2018-07-30 上午9.22.03.png
pan手勢(shì)是檢測(cè)move的,所以在touch move有響應(yīng)時(shí),pan手勢(shì)也進(jìn)行響應(yīng)
UISwipeGestureRecognizer

屏幕快照 2018-07-30 上午9.23.14.png
UILongPressGestureRecognizer

屏幕快照 2018-07-30 上午9.23.34.png