事件處理(一)

1> 事件處理簡介

  • 3大事件:(主要)觸摸事件、加速計(jì)事件、遠(yuǎn)程控制事件。

  • 什么是響應(yīng)者對(duì)象
    - 在iOS中不是任何對(duì)象都能處理事件,只有繼承了UIResponder的對(duì)象才能接收并處理事件。我們稱之為“響應(yīng)者對(duì)象”
    - UIApplication、UIViewController、UIView都繼承自UIResponder,因此它們都是響應(yīng)者對(duì)象,都能夠接收并處理事件

  • 為什么繼承UIResponder就能處理事件

    • UIResponder內(nèi)部提供了以下方法來處理事件
 1.觸摸事件
- (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;

2.加速計(jì)事件
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event;

3.遠(yuǎn)程控制事件
- (void)remoteControlReceivedWithEvent:(UIEvent *)event;
  • 想處理觸摸事件,應(yīng)該怎么辦
  • UIView是UIResponder的子類,可以覆蓋下列4個(gè)方法處理不同的觸摸事件
1.一根或者多根手指開始觸摸view,系統(tǒng)會(huì)自動(dòng)調(diào)用view的下面方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

2.一根或者多根手指在view上移動(dòng),系統(tǒng)會(huì)自動(dòng)調(diào)用view的下面方法(隨著手指的移動(dòng),會(huì)持續(xù)調(diào)用該方法)
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

3.一根或者多根手指離開view,系統(tǒng)會(huì)自動(dòng)調(diào)用view的下面方法
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

4.觸摸結(jié)束前,某個(gè)系統(tǒng)事件(例如電話呼入)會(huì)打斷觸摸過程,系統(tǒng)會(huì)自動(dòng)調(diào)用view的下面方法
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
提示:touches中存放的都是UITouch對(duì)象
  • 重點(diǎn)UITouch

  • 當(dāng)用戶用一根手指觸摸屏幕時(shí),會(huì)創(chuàng)建一個(gè)與手指相關(guān)聯(lián)的UITouch對(duì)象

  • 一根手指對(duì)應(yīng)一個(gè)UITouch對(duì)象

  • UITouch的作用
    保存著跟手指相關(guān)的信息,比如觸摸的位置、時(shí)間、階段

  • 當(dāng)手指移動(dòng)時(shí),系統(tǒng)會(huì)更新同一個(gè)UITouch對(duì)象,使之能夠一直保存該手指在的觸摸位置

  • 當(dāng)手指離開屏幕時(shí),系統(tǒng)會(huì)銷毀相應(yīng)的UITouch對(duì)象
    提示:iPhone開發(fā)中,要避免使用雙擊事件!

1.觸摸事件方法中的UITouch都是同一個(gè)對(duì)象,因?yàn)橐桓种笇?duì)應(yīng)一個(gè)UITouch.當(dāng)手指移動(dòng)或者抬起,并不會(huì)產(chǎn)生一個(gè)新的UITouch對(duì)象給你,而是改變UITouch里面的屬性,

1.默認(rèn)三個(gè)方法里面只能獲取到一個(gè)手指,為什么。
     UIView不支持多點(diǎn)觸控
2.怎么才能有兩個(gè)手指,兩個(gè)手指同時(shí)按,并且視圖支持多點(diǎn)觸控

3.UITouch的tapCount有什么用?
    可以判斷用戶當(dāng)前是雙擊還是單擊
4.UITouch的phase有什么用? 
   根據(jù)這個(gè)屬性,判斷當(dāng)前需要調(diào)用哪個(gè)處理事件方法,begin,move,end
  • 一般步驟

1.處理一個(gè)控件的觸摸事件步驟
1.1 自定義View
1.2 重寫touches方法

// NSSet:集合,無序
// 當(dāng)手指觸摸當(dāng)前控件的時(shí)候調(diào)用
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//    
//    NSLog(@"%d",touches.count);
//    // 隨機(jī)取一個(gè)
//    UITouch *touch = [touches anyObject];
//    // 獲取當(dāng)前點(diǎn)
//   CGPoint curP = [touch locationInView:self];
//     NSLog(@"%@",NSStringFromCGPoint(curP));
    NSLog(@"%s",__func__);
}

// 當(dāng)手指在當(dāng)前控件移動(dòng)的時(shí)候調(diào)用
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    
    // 獲取UITouch
    UITouch *touch = [touches anyObject];
    
    // 獲取當(dāng)前點(diǎn)
    CGPoint curP = [touch locationInView:self];
    
    // 獲取上一次點(diǎn)
    CGPoint preP = [touch previousLocationInView:self];
    
    // 獲取x軸偏移量
    CGFloat offsetX = curP.x - preP.x;
    // 獲取y軸偏移量
    CGFloat offsetY = curP.y - preP.y;
    
    // 修改當(dāng)前控件的位置
    // 修改形變
    // CGAffineTransformMakeTranslation:相對(duì)于最開始的位置,使用Make,會(huì)把之前的所有形變清空,從零開始形變
    // 相對(duì)于上一次的形變
    
    self.transform = CGAffineTransformTranslate(self.transform, offsetX, offsetY);
    
    NSLog(@"%s",__func__);
}

// 當(dāng)手指抬起的時(shí)候調(diào)用
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"%s",__func__);
}

// 當(dāng)觸摸事件被打斷的時(shí)候調(diào)用,比如打電話
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    
}
最后編輯于
?著作權(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),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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