swfit3.0之手勢解鎖

前言

1.gif

先來一個手勢解鎖的例子,熟悉一下swift3.0的一些基本改變。變化很大使用過程當中跌跌撞撞的,多多學習。git

demo下載地址

目錄

響應者對象
UITouch中的方法

響應者對象

在IOS中不是任何對象都可以處理事件,只有繼承了UIResponder的對象才能接收並處理事件,咱們稱之爲「響應者對象」 
UIApplication、UIViewController、UIView都繼承自UIResponder,因此都能成爲響應者對象
UIResponder內部提供瞭如下方法來處理事件 
>觸摸事件 
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event 
-(void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event 
-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event 
>加速計事件 
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent*)event 
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent*)event 
-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent*)event 
>遠程控制事件 
-(void)removeControlReceivedWithEvent:(UIEvent*)event 

UITouch中的方法

UITouch的方法
- (CGPoint)locationInView:(UIView*)view;
返回值表示觸摸在view上的位置
這裏返回的位置是針對view的座標系的(以view的左上角爲原點(0, 0))
調用時傳入的view參數爲nil的話,返回的是觸摸點在UIWindow的位置

- (CGPoint)previousLocationInView:(UIView*)view;
該方法記錄了前一個觸摸點的位置
touches和event參數
一次完整的觸摸過程,會經歷3個狀態:
觸摸開始:- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
觸摸移動:- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
觸摸結束:- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
觸摸取消(可能會經歷):- (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event

4個觸摸事件處理方法中,都有NSSet*touches和UIEvent*event兩個參數
一次完整的觸摸過程當中,只會產生一個事件對象,4個觸摸方法都是同一個event參數

若是兩根手指同時觸摸一個view,那麼view只會調用一次touchesBegan:withEvent:方法,touches參數中裝着2個UITouch對象

若是這兩根手指一前一後分開觸摸同一個view,那麼view會分別調用2次touchesBegan:withEvent:方法,而且每次調用時的touches參數中只包含一個UITouch對象

根據touches中UITouch的個數能夠判斷出是單點觸摸仍是多點觸摸
相關文章
相關標籤/搜索