在iOS中UIView是繼承于UIResponder的,而UIResponder是專(zhuān)門(mén)用來(lái)響應(yīng)用戶(hù)的操作處理各種事件的降狠,包括觸摸事件(Touch Events)西篓、運(yùn)動(dòng)事件(Motion Events)锣光、遠(yuǎn)程控制事件(Remote Control Events,如插入耳機(jī)調(diào)節(jié)音量觸發(fā)的事件)踪危,而很多我們常用的類(lèi)也繼承于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;//離開(kāi)屏幕
- (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];