iOS中的事件處理,iOS事件處理

來源:互聯網
上載者:User

iOS中的事件處理,iOS事件處理

前言:iOS中事件處理,是一個很重要也很難得地方。涉及到響應者鏈的地方的面試題,很多工作兩三年的老鳥也未必能回答的很專業。這裡詳細介紹一下iOS中的事件處理,以及響應者鏈。

1. 三大事件2. 響應者對象
  • 在iOS中不是任何對象都能處理事件,只有繼承了UIResponder的對象才能接收並處理事件。我們稱之為 響應者對象
  • UIApplication、UIViewController、UIView都繼承自UIResponder,因此它們都是響應者對象,都能夠接收並處理事件
2.1 UIResponder內部提供了以下方法來處理事件
  • 觸摸事件
    - (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;
  • 加速計事件
    - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;    - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;    - (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;
  • 遠端控制事件
- (void)remoteControlReceivedWithEvent:(UIEvent *)event;
2.2 UIView的觸摸事件處理
  • UIView是UIResponder的子類,可以覆蓋下列4個方法處理不同的觸摸事件
//根或者多根手指開始觸摸view,系統會自動調用view的下面方法- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event//一根或者多根手指在view上移動,系統會自動調用view的下面方法(隨著手指的移動,會持續調用該方法)- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event//一根或者多根手指離開view,系統會自動調用view的下面方法- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event//觸摸結束前,某個系統事件(例如電話呼入)會打斷觸摸過程,系統會自動調用view的下面方法- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

提示:touches中存放的都是UITouch對象

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

  • **一根手指對應一個**UITouch對象

UITouch的作用
- 儲存著跟手指相關的資訊,比如觸摸的位置、時間、階段

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

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

    提示:iPhone開發中,要避免使用雙擊事件

UITouch的屬性

//觸摸產生時所處的視窗@property(nonatomic,readonly,retain) UIWindow    *window;//觸摸產生時所處的視圖@property(nonatomic,readonly,retain) UIView      *view;//短時間內點按螢幕的次數,可以根據tapCount判斷單擊、雙擊或更多的點擊@property(nonatomic,readonly) NSUInteger          tapCount;//記錄了觸摸事件產生或變化時的時間,單位是秒,用的很少@property(nonatomic,readonly) NSTimeInterval      timestamp;//當前觸摸事件所處的狀態@property(nonatomic,readonly) UITouchPhase        phase;

UITouch的方法

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

  • 傳回值表示觸摸在view上的位置
  • 這裡返回的位置是針對view的座標系的(以view的左上方為原點(0, 0))
  • 調用時傳入的view參數為nil的話,返回的是觸摸點在UIWindow的位置

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

  • 該方法記錄了前一個觸摸點的位置
2.4 UIEvent
  • 每產生一個事件,就會產生一個UIEvent對象

  • UIEvent:稱為事件對象,記錄事件產生的時刻和類型

常見屬性
1.事件類型
@property(nonatomic,readonly) UIEventType type;
@property(nonatomic,readonly) UIEventSubtype subtype;

2.事件產生的時間
@property(nonatomic,readonly) NSTimeInterval timestamp;

UIEvent還提供了相應的方法可以獲得在某個view上面的觸摸對象(UITouch)

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的個數可以判斷出是單點觸摸還是多點觸摸

3. 事件的產生和傳遞3.1 事件傳遞的規則3.2 事件傳遞樣本

觸摸事件的傳遞是從父控制項傳遞到子控制項

點擊了綠色的view:
UIApplication -> UIWindow -> 白色 -> 綠色
點擊了藍色的view:
UIApplication -> UIWindow -> 白色 -> 橙色 -> 藍色
點擊了黃色的view:
UIApplication -> UIWindow -> 白色 -> 橙色 -> 藍色 -> 黃色

如果父控制項不能接收觸摸事件,那麼子控制項就不可能接收到觸摸事件(掌握)

  • 如何找到最合適的控制項來處理事件:

提示:UIImageView的userInteractionEnabled預設就是NO,因此UIImageView以及它的子控制項預設是不能接收觸摸事件的

4. 響應者鏈條4.1 觸摸事件處理的詳細過程4.2 響應者鏈條
  • 響應者鏈條:是由多個響應者對象串連起來的鏈條
  • 作用:能很清楚的看見每個響應者之間的聯絡,並且可以讓一個事件多個對象處理。
  • 響應者對象:能處理事件的對象
4.3 事件傳遞的完整過程4.4 響應者鏈的事件傳遞過程5.執行個體講解

實現以下一個案例:
黃色的View在按鈕之上,View的透明度為0.5,現在要求當點擊在View上且在按鈕上的時候,響應按鈕

#import "YellowView.h"@interface YellowView ()@property (nonatomic, weak) IBOutlet UIButton *btn;@end@implementation YellowView//用來測試UIView有沒有被點擊- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    NSLog(@"%s",__func__);}- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{    //將當前的View座標轉換到Button上    CGPoint btnP = [self convertPoint:point toView:_btn];    // 判斷下當前點在不在按鈕,如果在按鈕上,返回按鈕    if ([_btn pointInside:btnP withEvent:event]) {        return _btn;    }else{        return [super hitTest:point withEvent:event];    }}@end

測試結果:
1. 單擊黃色View以內,Button以外的地方,列印

2. 單擊按鈕,按鈕的title顏色發生變化,表明Button響應了

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.