iOS開發UI篇—事件處理簡單介紹1

來源:互聯網
上載者:User

標籤:

iOS開發UI篇—事件處理簡單介紹

一、事件處理簡單介紹

說明:ios中的事件

在使用者使用app過程中,會產生各種各樣的事件 ,iOS中的事件可以分為3大類型 :

1.響應者對象

在iOS中不是任何對象都能處理事件,只有繼承了UIResponder的對象才能接收並處理事件。我們稱之為“響應者對象”UIApplication、UIViewController、UIView都繼承自UIResponder,因此它們都是響應者對象,都能夠接收並處理事件

2.UIResponder

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;

 

3.UIView的觸摸事件處理

UIView是UIResponder的子類,可以覆蓋下列4個方法處理不同的觸摸事件

(1)一根或者多根手指開始觸摸view,系統會自動調用view的下面方法

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

(2)一根或者多根手指在view上移動,系統會自動調用view的下面方法(隨著手指的移動,會持續調用該方法)

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

(3)一根或者多根手指離開view,系統會自動調用view的下面方法

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

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

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

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

 

4.UITouch

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

一根手指對應一個UITouch對象

(1)UITouch的作用

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

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

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

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

(2)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;

 

(3)UITouch的方法

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

傳回值表示觸摸在view上的位置

這裡返回的位置是針對view的座標系的(以view的左上方為原點(0, 0))

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

 

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

該方法記錄了前一個觸摸點的位置

5.UIEvent

每產生一個事件,就會產生一個UIEvent對象

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

常見屬性

事件類型

@property(nonatomic,readonly) UIEventType     type;

@property(nonatomic,readonly) UIEventSubtype  subtype;

事件產生的時間

@property(nonatomic,readonly) NSTimeInterval  timestamp;

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

 

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

二、程式碼範例

代碼說明:該程式使用事件處理特性完成一個簡單的小功能,點擊介面上的view,讓view可以隨著滑鼠(手指)的移動進行拖動。

代碼:

 9 #import "YYview.h"10 11 @implementation YYview12 13 // 觸摸事件完整的調用過程touchesBegan --> touchesMoved --> touchesEnded14 // 當手指觸摸view的時候調用15 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event16 {17     NSLog(@"手指觸摸view");18 }19 20 //手指移動的時候調用21 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event22 {23     NSLog(@"手指移動");24     //擷取UITouch對象25     UITouch *touch=[touches anyObject];26     27     //擷取手指上個位置28     CGPoint prePoint=[touch previousLocationInView:self];29     //列印位置資訊查看30     NSLog(@"prePoint=%@",NSStringFromCGPoint(prePoint));31     32     //擷取手指當前的位置33     CGPoint currentPoint=[touch locationInView:self];34     //列印位置資訊查看35     NSLog(@"currentPoint=%@",NSStringFromCGPoint(currentPoint));36     37     //計算手指移動的距離38     CGFloat moveX=currentPoint.x-prePoint.x;39     CGFloat moveY=currentPoint.y-prePoint.y;40     41     //設定view的x和y值42     //注意:不能直接改變屬性內部結構體的某個屬性值43     CGPoint temp=self.center;44     temp.x+=moveX;45     temp.y+=moveY;46     self.center=temp;47     48 }49 50 //手指離開的時候調用51 -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event52 {53     NSLog(@"手指離開");54 }55 56 //觸摸事件被打斷的時候調用(如來電等)57 -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event58 {59     60 }61 @end

程式:

部分列印效果:

補充說明:

1 //    UITouch *touch = [touches anyObject];2     // 擷取手指觸摸的位置3     // 如果locationInView傳遞self, 將來擷取出來的位置,是以自己的左上方為原點(00)4     // 如果傳遞的是父視圖,將來擷取出來的位置,是以父視圖的左上方為原點(00)5 //    CGPoint point = [touch locationInView:self.superview];6 //    NSLog(@"touchesBegan %@", NSStringFromCGPoint(point));7     8     // 擷取手指點擊的次數9 //    NSLog(@"tapCount = %d", touch.tapCount);

 

iOS開發UI篇—事件處理簡單介紹1

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.