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對應的控件拖線,(反着拖)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章