文章搬運(yùn)來(lái)源:https://blog.csdn.net/Calvin_zhou/article/details/110931665
作者:PGzxc(如有侵權(quán),聯(lián)系作者,立即刪除)
對(duì)iOS開發(fā)感興趣,可以看一下作者的iOS交流群:812157648,大家可以在里面吹水、交流相關(guān)方面的知識(shí),群里還有我整理的有關(guān)于面試的一些資料,歡迎大家加群,大家一起開車
一 IOS中的事件
- 在用戶使用app過程中,會(huì)產(chǎn)生各種各樣的事件
- IOS中的事件可以分為3大類型:觸摸事件、加速計(jì)事件、遠(yuǎn)程控制事件
二 響應(yīng)者對(duì)象
- 在iOS中不是任何對(duì)象都能處理事件,只有繼承了UIResponder的對(duì)象才能接收并處理事件。我們稱之為“響應(yīng)者對(duì)象”
- UIApplication、UIViewController、UIView都能繼承自UIResponder,因此它們都是響應(yīng)者對(duì)象,都能接收并處理事件
三 UIResponder
3.1 UIResponder內(nèi)部提供了以下方法來(lái)處理事件
觸摸事件
-(void)touchBegan:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchMoved:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchEnded:(NSSet *)touches withEvent:(UIEvent *)event;
-(void)touchCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
加速計(jì)事件
-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;
-(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;
遠(yuǎn)程控制事件
-(void)remoteControlReceivedWithEvent:(UIEvent *)event;
四 UIView的觸摸事件處理
4.1 UIView是UIResponder的子類,可以實(shí)現(xiàn)下列4個(gè)方法處理不同的觸摸事件
一根或者多根手指開始觸摸view,系統(tǒng)會(huì)自動(dòng)調(diào)用view的下面方法
- -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
一根或多根手指在view上移動(dòng),系統(tǒng)會(huì)自動(dòng)調(diào)用view的下面方法(隨著手指的移動(dòng)持續(xù)調(diào)用該方法)
- -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
一根或者多根手指離開view,系統(tǒng)會(huì)自動(dòng)調(diào)用view的下面方法
- -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
觸摸結(jié)束前,某個(gè)系統(tǒng)事件(例如電話呼入)會(huì)打斷觸摸過程,系統(tǒng)會(huì)自動(dòng)調(diào)用下面的方法
- -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
4.2 說(shuō)明
- touches中存放的都是UITouch對(duì)象
五 UITouch
5.1 什么是UITouch
- 當(dāng)用戶用一根手指觸摸屏幕時(shí),會(huì)創(chuàng)建一個(gè)與手指相關(guān)聯(lián)的UITouch對(duì)象
- 一根手指對(duì)應(yīng)一個(gè)UITouch對(duì)象
5.2 UITouch的作用
- 保存著跟手指相關(guān)的信息,比如觸摸的位置、時(shí)間、階段
5.3 手指移動(dòng)時(shí)
- 當(dāng)手指移動(dòng)時(shí),系統(tǒng)會(huì)更新同一個(gè)UITouch對(duì)象,使之能夠一直保存該手指的觸摸位置
5.4 常用的屬性
觸摸產(chǎn)生時(shí)所處的窗口
@property(nonatomic,readonly,retain)UIWindow *window;
觸摸產(chǎn)生時(shí)所處的視圖
@property(nonatomic,readonly,retain)UIView *view;
短時(shí)間內(nèi)點(diǎn)擊屏幕的次數(shù),可以根據(jù)tapCount判斷單擊、雙擊或更多的點(diǎn)擊
@property(nonatomic,readonly)NSUInteger tapCount;
記錄了觸摸事件產(chǎn)生或變化的時(shí)間,單位是秒
@property(nonatomic,readonly)NSTimeInterval timestamp;
當(dāng)前觸摸事件所處的狀態(tài)
@property(nonatomic,readonly)UITouchPhase phase;
5.5 常用的方法
touches.count
- 觸摸的手指數(shù)(勾選Multiple Touch)
touch.tapCount
- 點(diǎn)擊屏幕的次數(shù)
touch.phase(枚舉類型)
typedef NS_ENUM(NSInteger, UITouchPhase) {
UITouchPhaseBegan, // whenever a finger touches the surface.
UITouchPhaseMoved, // whenever a finger moves on the surface.
UITouchPhaseStationary, // whenever a finger is touching the surface but hasn't moved since the previous event.
UITouchPhaseEnded, // whenever a finger leaves the surface.
UITouchPhaseCancelled,
六 view拖動(dòng)
6.1 思路
- 獲取當(dāng)前手指的位置
- 獲取上一個(gè)點(diǎn)
- 計(jì)算偏移量(x軸和y軸)
- 獲取視圖Center,根據(jù)偏移量獲取最新位置
6.2 代碼
//觸摸移動(dòng)
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UITouch *touch=[touches anyObject];
//獲取當(dāng)前的位置
CGPoint current=[touch locationInView:self];
CGPoint pre=[touch previousLocationInView:self];
//x軸的偏移量
CGFloat offsetX=current.x-pre.x;
CGFloat offsetY=current.y-pre.y;
CGPoint center=self.center;
center.x+=offsetX;
center.y+=offsetY;
self.center=center;
//NSLog(@"%d",touch.phase);
//NSLog(@"%s---%p",__func__,touch);
}
6.3 效果圖
