IOS UITouch 触摸事件,UIResponder 的作用
生活随笔
收集整理的這篇文章主要介紹了
IOS UITouch 触摸事件,UIResponder 的作用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
IOS 中事件響應皆是通過UIResponder。我們可以執行UIResponder協議響應事件。這里我們用UIViewController做示例,UIViewController默認實現UIResponder協議。
// UIViewController.m - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {NSLog(@"屏幕被手指按下了");// 獲取手指列表NSArray<UITouch*>* toucheList = [touches allObjects];for (int i = 0; i < toucheList.count; i ++) {UITouch* touch = toucheList[i];NSLog(@"獲取手指15s內同一位置點擊次數 -> 手指 %d 15s內點擊了 %zd 次", i, touch.tapCount);} }- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {NSLog(@"手指移動啦");// 獲取手指列表UITouch* touch = [touches allObjects][0];// 獲取手指相對于指定視圖的x,y軸CGPoint position = [touch locationInView:self.view];NSLog(@"手指坐標為x = %f,y = %f", position.x, position.y); }- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {NSLog(@"手指離開了"); }- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {NSLog(@"觸摸過程被其他行為異常中斷"); }UIResponder 協議函數
- - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event 當手指觸摸屏幕觸發
- - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event 當手指觸摸并產生移動觸摸
- - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event 當手指離開屏幕觸發
- - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event 當手指觸摸過程被其他行為中斷等異常情況觸發
- - (void)pressesBegan:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event API_AVAILABLE(ios(9.0)) 用戶用力按下屏幕開始觸發
- - (void)pressesChanged:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event API_AVAILABLE(ios(9.0)) 用戶用力按下屏幕發生壓力變化觸發
- - (void)pressesEnded:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event API_AVAILABLE(ios(9.0)) 用戶按壓結束觸發
- - (void)pressesCancelled:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event 用戶按壓行為被打斷觸發,IOS9以上支持
- - (void)motionBegan:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event 陀螺儀或加速設備開始發生改變觸發
- - (void)motionEnded:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event 陀螺儀或加速設備發生改變過程觸發
- - (void)motionCancelled:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event 陀螺儀或加速設備改變過程被其他行為中斷異常觸發
UITouch 屬性
| timestamp | NSTimeInterval | 手指當前觸摸的時間 | |
| phase | UITouchPhase | 手指當前處于的行為狀態 | |
| tapCount | NSUInteger | 手指15秒內同一位置觸摸的次數 | 0 |
| type | UITouchType | 手指是間接還是直接觸摸屏幕 | |
| type | UITouchType | 手指是間接還是直接觸摸屏幕 | |
| majorRadius | CGFloat | 手指觸摸屏幕的半徑 | |
| maximumPossibleForce | CGFloat | 設備最大壓力力度 | |
| maximumPossibleForce | CGFloat | 設備最大壓力力度 |
UITouch API
- - (CGPoint)locationInView:(nullable UIView *)view 手指當前相對于指定視圖的位置
- - (CGPoint)previousLocationInView:(nullable UIView *)view 手指上一次相對于指定視圖的位置
總結
以上是生活随笔為你收集整理的IOS UITouch 触摸事件,UIResponder 的作用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html 页面元素id不唯一,规定htm
- 下一篇: java 怎么从性能上优化代码_月薪上万