2.0 觸摸事件,2.0觸摸

來源:互聯網
上載者:User

2.0 觸摸事件,2.0觸摸

UIView不接收觸摸事件的三種情況:

 

1、不接收使用者互動userInteractionEnabled = NO
2、隱藏hidden = YES
3、透明alpha = 0.0 ~ 0.01
4. 如果子視圖的位置超出了父視圖的有效範圍, 那麼子視圖也是無法與使用者互動的, 即使設定了父視圖的 clipsToBounds = NO, 可以看到, 但是也是無法與使用者互動的 提示:UIImageView的userInteractionEnabled預設就是NO,因此UIImageView以及它的子控制項預設是不能接收觸摸事件的
   觸摸事件中的方法: 
 1 // 手指按下的時候調用 2 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event; 3  4 // 手指移動的時候調用 5 - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullableUIEvent *)event; 6  7 // 手指抬起的時候調用 8 - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullableUIEvent *)event; 9 10 // 取消(非正常離開螢幕,意外中斷事件)11 - (void)touchesCancelled:(nullableNSSet<UITouch *> *)touches withEvent:(nullableUIEvent *)event;
1 // 3D Touch相關方法,當前觸摸對象觸摸時力量改變所觸發的事件,傳回值是UITouchPropertyie2 - (void)touchesEstimatedPropertiesUpdated:(NSSet * _Nonnull)touches NS_AVAILABLE_IOS(9_1);

 

相關代碼:

 1 #import “TDView.h" 2  3 @implementation TDView 4  5 // 手指觸摸到這個view的時候調用 6 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 7  8   UITouch *t = touches.anyObject; 9 10   NSLog(@"%ld", t.tapCount); // 點擊這個響應者對象的次數11 12   NSLog(@"%@", t.window); // 點擊這個響應者對象所在的window13   NSLog(@"%@", self.window);14   NSLog(@"%@", [UIApplication sharedApplication].keyWindow);15 16   NSLog(@"%s", __func__);17 }18 19 // 手指在這個view上移動的時候調用20 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {21   // 擷取觸摸對象22   UITouch *t = touches.anyObject;23 24   // 擷取上一個點的位置25   CGPoint lastP = [t previousLocationInView:self.superview];26   NSLog(@"%@----上一個點的位置", NSStringFromCGPoint(lastP));27 28   // 擷取當前點的位置29   CGPoint p = [t locationInView:self.superview];30   NSLog(@"%@----當前點的位置", NSStringFromCGPoint(p));31 }32 33 // 手指離開這個view的時候調用34 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {35       NSLog(@"%s", __func__);36 }37 38 // 取消(非正常離開螢幕)39 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {40       NSLog(@"%s", __func__);41 }42 43 @end

 

補充:關於NSSet和 NSArray 
  如何訪問 如何取值 如何遍曆 效率                    應用情境
  NSSet集合 無序訪問,不重複的。 存放到 NSSet 中的內容並不會排序與添加順序也沒有關係 取集合裡面任意一個元素 anyObject 通過 anyObject 來訪問單個元素 for in  效率高 (1)比如重用 Cell 的時候, 從緩衝池中隨便擷取一個就可以了, 無需按照指定順序來擷取 (2)當需要把資料存放到一個集合中, 然後判斷集合中是否有某個對象的時候
  NSArray數組 有序訪問,可以有重複對象。 對象的順序是按照添加的順序來儲存的  通過下標來訪問 for  forin    當需要把資料存放到一個集合中, 然後判斷集合中是否有某個對象的時候
  
如有疑問,請發送郵件至 shorfng@126.com 聯絡我。 By:藍田(Loto) 

相關文章

聯繫我們

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