IOS開發(fā)之——事件處理-View拖動(dòng)

文章搬運(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 效果圖

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • iOS開發(fā)中的事件處理 理論非原創(chuàng),是對(duì)網(wǎng)上資料的整理以及Demo驗(yàn)證 一. UIResponder 1.1 事件...
    喪心病狂樂閱讀 802評(píng)論 0 0
  • 在開發(fā)過程中,大家或多或少的都會(huì)碰到令人頭疼的手勢(shì)沖突問題,正好前兩天碰到一個(gè)類似的bug,于是借著這個(gè)機(jī)會(huì)了解了...
    閆仕偉閱讀 5,670評(píng)論 2 23
  • 事件 移動(dòng)應(yīng)用中常用的事件 處理事件的條件: 在iOS中,不是任何對(duì)象都能處理事件,只有繼承了UIResponde...
    叫我Dragon閱讀 1,892評(píng)論 0 1
  • 前言: 事件 是界面交互(或人機(jī)交互)的最基本組成之一。沒有它手機(jī)上的App就失去了存在的意義。 一個(gè)事件的周期:...
    ildream閱讀 5,911評(píng)論 6 13
  • 久違的晴天,家長(zhǎng)會(huì)。 家長(zhǎng)大會(huì)開好到教室時(shí),離放學(xué)已經(jīng)沒多少時(shí)間了。班主任說(shuō)已經(jīng)安排了三個(gè)家長(zhǎng)分享經(jīng)驗(yàn)。 放學(xué)鈴聲...
    飄雪兒5閱讀 7,788評(píng)論 16 22

友情鏈接更多精彩內(nèi)容