標籤:
A.需要掌握和練習的1.介紹事件類型
2.通過按鈕的事件處理引出view的事件處理
3.響應者對象 --> UIResponder --> UIView
4.view的拖拽
* 實現觸摸方法,列印查看
* 介紹touches和UIEvent參數
* 擴充:點哪去哪
5.塗鴉
6.手勢解鎖
7.事件的產生和傳遞
8.響應者鏈條9.手勢
B.概念1.iOS有3種主要事件
2.響應者對象 在iOS中不是任何對象都能處理事件,只有繼承了UIResponder的對象才能接收並處理事件。我們稱之為“響應者對象” UIApplication、UIViewController、UIView都繼承自UIResponder,因此它們都是響應者對象,都能夠接收並處理事件
3.UIResponderUIResponder內部提供了以下方法來處理事件
觸摸事件
- (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;
5.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對象
6.UITouch當使用者用一根手指觸控螢幕幕時,會建立一個與手指相關聯的UITouch對象
一根手指對應一個UITouch對象
UITouch的作用
儲存著跟手指相關的資訊,比如觸摸的位置、時間、階段
當手指移動時,系統會更新同一個UITouch對象,使之能夠一直儲存該手指在的觸摸位置
當手指離開螢幕時,系統會銷毀相應的UITouch對象
提示:iPhone開發中,要避免使用雙擊事件!
7.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;
8.UITouch方法- (CGPoint)locationInView:(UIView *)view;
傳回值表示觸摸在view上的位置
這裡返回的位置是針對view的座標系的(以view的左上方為原點(0, 0))
調用時傳入的view參數為nil的話,返回的是觸摸點在UIWindow的位置
- (CGPoint)previousLocationInView:(UIView *)view;
該方法記錄了前一個觸摸點的位置
9.UIEvent每產生一個事件,就會產生一個UIEvent對象
UIEvent:稱為事件對象,記錄事件產生的時刻和類型
常見屬性
事件類型
@property(nonatomic,readonly) UIEventType type;
@property(nonatomic,readonly) UIEventSubtype subtype;
事件產生的時間
@property(nonatomic,readonly) NSTimeInterval timestamp;
UIEvent還提供了相應的方法可以獲得在某個view上面的觸摸對象(UITouch)
10.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的個數可以判斷出是單點觸摸還是多點觸摸
11.事件的產生和傳遞
12.可以拖動的UIView可以拖曳的UIView色塊
1 // 2 // MoveView.m 3 // TouchMove 4 // 5 // Created by hellovoidworld on 15/1/10. 6 // Copyright (c) 2015年 hellovoidworld. All rights reserved. 7 // 8 9 #import "MoveView.h"10 11 @implementation MoveView12 13 /** 正在拖曳 */14 - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {15 // 取得當前的touch對象16 UITouch *touch = [touches anyObject];17 18 // 上一個位置19 CGPoint prep = [touch previousLocationInView:self];20 // 現在位置21 CGPoint p = [touch locationInView:self];22 // 現在的x座標23 CGFloat x = self.frame.origin.x + (p.x - prep.x);24 // 現在的y座標25 CGFloat y = self.frame.origin.y + (p.y - prep.y);26 // 改變位置27 self.frame = CGRectMake(x, y, self.frame.size.width, self.frame.size.height);28 }29 30 @end31
[iOS UI進階 - 3.0] 觸摸事件的基本處理