天天看點

UI進階第九發:iOS常用事件

1.IOS事件類型

1>觸摸事件

2>加速計事件

3>遠端控制器

2.響應者對象: UiResponder

1>含義與作用:

繼承了UiResponder 的對象(UIApplication,UIView,UIewController)

隻有繼承了UiResponder的對象才可以接收并處理事件

2>UiResponder對象方法

one:觸摸事件

開始觸摸: - (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;

two:加速計事件

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;

- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;

three:遠端控制事件

- (void)remoteControlReceivedWithEvent:(UIEvent *)event;

3.UIView事件

是UIResponder的子類,使用UIview事件步驟:自定義View,繼承UIView實作方法。

1>開始觸摸view

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

2>在view上移動(随着手指的移動,會持續調用該方法)

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

3>手指離開view

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

4>觸摸結束前,某個系統事件(例如電話呼入)會打斷觸摸過程,系統會自動調用view的下面方法

(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

5> touches參數:儲存的就是UITouch對象

4.UITouch對象

1>當使用者用一根手指觸摸螢幕時,會建立一個與手指相關聯的UITouch對象

2>一根手指對應一個UITouch對象

3>UITouch的作用

one:儲存着跟手指相關的資訊,比如觸摸的位置、時間、階段

two:當手指移動時,系統會更新同一個UITouch對象,使之能夠一直儲存該手指的觸摸位置。

      (注意:在銷毀前始終隻建立一個對象,不斷更新)

three:當手指離開螢幕時,系統會銷毀相應的UITouch對象

注意:iPhone開發中,要避免使用輕按兩下事件

one:因為touchs參數隻對應一個UITouch對象,一個手指對應一個UITouch

two:想要使用輕按兩下事件,将storyboard的屬性勾上,允許輕按兩下事件的發生

5.UITouch屬性

1>目前觸摸時的視窗

@property(nonatomic,readonly,retain) UIWindow      *window;

2>目前觸摸時的視圖

@property(nonatomic,readonly,retain) UIView      *view;

3>短時間内點按螢幕的次數,可以根據tapCount判斷單擊、輕按兩下或更多的點選

@property(nonatomic,readonly) NSUInteger          tapCount;

4>記錄了觸摸事件産生或變化時的時間(機關:秒)

@property(nonatomic,readonly) NSTimeInterval      timestamp;

5>目前觸摸事件所處的狀态

@property(nonatomic,readonly) UITouchPhase        phase;

UITouchPhase是一個枚舉類型,包含:

UITouchPhaseBegan(觸摸開始)

UITouchPhaseMoved(接觸點移動)

UITouchPhaseStationary(接觸點無移動)

UITouchPhaseEnded(觸摸結束)

UITouchPhaseCancelled(觸摸取消)

6.UITouch方法

1>傳回目前觸摸在view上的位置

- (CGPoint)locationInView:(UIView *)view;

2>傳回上一個觸摸點的位置

-(CGPoint)previousLocationInView:(UIView *)view;

注意:調用時傳入的view參數為nil的話,傳回的是觸摸點在UIWindow的位置

7.UIEvent

1>每産生一個事件,就會産生一個UIEvent對象

2>UIEvent:稱為事件對象,記錄事件産生的時刻和類型

3>常見屬性

one:事件類型: @property(nonatomic,readonly) UIEventType    type;

two:      @property(nonatomic,readonly) UIEventSubtype  subtype;

three.事件産生時間     @property(nonatomic,readonly) NSTimeInterval  timestamp;

8.事件中的touches和event參數

1>一次完整的觸摸過程,會經曆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

2>4個觸摸事件處理方法中,都有NSSet *touches和UIEvent *event兩個參數.

     一次完整的觸摸過程中,隻會産生一個事件對象,4個觸摸方法都是同一個event參數.

3>如果兩根手指同時觸摸一個view,那麼view隻會調用一次touchesBegan:withEvent:方法

    touches參數中裝着2個UITouch對象

4>如果兩根手指一前一後分開觸摸同一個view,那麼view會分别調用2次touchesBegan:withEvent:方法,并且每次調用時的touches參數中隻包含一個UITouch對象

9.補充:

1>NSSset和NSArray集合差別: NSSset無序(取東西:[set anyObject]),NSArray有序

2>__func__: 判斷目前方法在哪個類中調用

3>技巧:如果在storyboard中向類中拖線拖不了,那麼可以先在類中把屬性建立好,在類中向storyboard對應的控件拖線,(反着拖)