iOS開發(fā)之觸摸事件以及手勢
iOS中的事件分為三類:觸摸事件、加速計事件、遠程控制事件。只有繼承了UIResponder的對象才能接收并處理事件,稱之為“響應(yīng)者對象”。UIApplication、UIViewController、UIView都繼承自UIResponder。UIResponder內(nèi)部提供的方法來處理事件:
觸摸事件:touchesBegan、touchesMoved、touchesEnded、touchesCancelled
加速計事件:motionBegan、motionEnded、motionCancelled
遠程控制事件:remoteControlReceivedWithEvent
UIVeiw的觸摸事件處理過程:
/** * 當手指開始觸摸view時調(diào)用 * * @param touches <#touches description#> * @param event <#event description#> */ - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSLog(@"%s",__func__); } /** * 當手指在view上移動時調(diào)用 * * @param touches <#touches description#> * @param event <#event description#> */ - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSLog(@"%s",__func__); } /** * 當手指離開view時調(diào)用 * * @param touches <#touches description#> * @param event <#event description#> */ - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSLog(@"%s",__func__); } /** * 當觸摸事件被系統(tǒng)事件打斷時調(diào)用 * * @param touches <#touches description#> * @param event <#event description#> */ - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSLog(@"%s",__func__); }
一次觸摸動作必然會調(diào)用touchesBeagn、touchesMoved和touchesEnded這三個方法。
說到這幾個觸摸方法,首先要知道UITouch這個對象。當一根手指觸摸屏幕時就會產(chǎn)生一個與之關(guān)聯(lián)的UITouch對象,一根手指對應(yīng)一個UITouch對象。這個對象里面保存著這次觸摸的信息,比如觸摸的位置,時間,階段等,當手指移動時,系統(tǒng)會更新同一個UITouch對象。使其能一直保存該手指所在的觸摸位置信息。當手指離開屏幕時,系統(tǒng)會銷毀對應(yīng)的UITouch對象。
@interface UITouch : NSObject @property(nonatomic,readonly) NSTimeInterval timestamp; @property(nonatomic,readonly) UITouchPhase phase; @property(nonatomic,readonly) NSUInteger tapCount; // touch down within a certain point within a certain amount of time // majorRadius and majorRadiusTolerance are in points // The majorRadius will be accurate +/- the majorRadiusTolerance @property(nonatomic,readonly) CGFloat majorRadius NS_AVAILABLE_IOS(8_0); @property(nonatomic,readonly) CGFloat majorRadiusTolerance NS_AVAILABLE_IOS(8_0); @property(nullable,nonatomic,readonly,strong) UIWindow *window; @property(nullable,nonatomic,readonly,strong) UIView *view; @property(nullable,nonatomic,readonly,copy) NSArray <UIGestureRecognizer *> *gestureRecognizers NS_AVAILABLE_IOS(3_2); //獲取當前位置 - (CGPoint)locationInView:(nullable UIView *)view; //獲取上一個觸摸點的位置 - (CGPoint)previousLocationInView:(nullable UIView *)view; // Force of the touch, where 1.0 represents the force of an average touch @property(nonatomic,readonly) CGFloat force NS_AVAILABLE_IOS(9_0); // Maximum possible force with this input mechanism @property(nonatomic,readonly) CGFloat maximumPossibleForce NS_AVAILABLE_IOS(9_0); @end
eg:讓一個view隨著手指的移動而移動
/** * 當手指在view上移動時調(diào)用 * * @param touches <#touches description#> * @param event <#event description#> */ - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSLog(@"%s",__func__); //獲取UITouch對象 UITouch *touch = [touches anyObject]; //獲取當前點的位置 CGPoint curP = [touch locationInView:self]; //獲取上一個點的位置 CGPoint preP = [touch previousLocationInView:self]; //計算x的偏移量 CGFloat offsetX = curP.x - preP.x; //計算y的偏移量 CGFloat offsetY = curP.y = preP.y; //修改view的位置 self.transform = CGAffineTransformTranslate(self.transform, offsetX, offsetY); }
就是根據(jù)UITouch對象中保存的位置信息來實現(xiàn)的。
事件的產(chǎn)生和傳遞:
當觸摸事件產(chǎn)生后,系統(tǒng)會將該事件添加到一個由UIApplication管理的事件隊列中去。UIApplication會從隊列中取出最前面的事件,發(fā)送給應(yīng)用程序的主窗口的處理。主窗口會在視圖層次結(jié)構(gòu)中,找一個最合適的視圖并調(diào)用touches方法來處理觸摸事件。觸摸事件的傳遞是從父控件傳遞到子控件。如果父控件不能接收到觸摸事件,那么子控件就不可能 接收到觸摸事件。
如何找到最合適的控件來處理事件?首先判斷自己是否能接收觸摸事件?觸摸點是否在自己身上?從后往前遍歷子控件,重復(fù)之前的兩個步驟,如果沒有符合條件的子控件,那么就自己最合適處理。
控件用hitTest:withEvent:方法來尋找最合適的view,用pointInside這個方法判斷這個點在不在方法調(diào)用者即控件身上。
hitTest方法的底層實現(xiàn):
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { //判斷當前控件是否能接收觸摸事件 if (self.userInteractionEnabled == NO || self.hidden == YES || self.alpha <= 0.01) { return nil; } //判斷觸摸點是否在當前控件上 if ([self pointInside:point withEvent:event] == NO) { return nil; } //從后往前遍歷自己的子控件 NSInteger count = self.subviews.count; for (NSInteger i = count - 1; i >= 0; i--) { UIView *childView = self.subviews[i]; //把當前控件上的坐標系轉(zhuǎn)換成子控件上的坐標系 CGPoint childPoint = [self convertPoint:point toView:childView]; //遞歸調(diào)用hitTest方法尋找最合適的view UIView *fitView = [childView hitTest:childPoint withEvent:event]; if (fitView) { return fitView; } } //循環(huán)結(jié)束,沒有比自己更合適的view,返回自己 return self; }
然而使用touches方法監(jiān)聽觸摸事件是有缺點的,比如要自定義view,所以iOS3.2之后蘋果推出了手勢識別功能UIGestureRecognizer。UIGestureRecognizer是一個抽象類,它的子類才能處理具體的某個手勢。
具體有以下幾種手勢:
//點按手勢 // UITapGestureRecognizer *tap = [UITapGestureRecognizer alloc]initWithTarget:<#(nullable id)#> action:<#(nullable SEL)#> //長按手勢 默認是觸發(fā)兩次 // UILongPressGestureRecognizer *longP = [UILongPressGestureRecognizer alloc]initWithTarget:<#(nullable id)#> action:<#(nullable SEL)#> //輕掃手勢 默認方向是往右 // UISwipeGestureRecognizer *swipe = [UISwipeGestureRecognizer alloc]initWithTarget:<#(nullable id)#> action:<#(nullable SEL)#> //旋轉(zhuǎn)手勢 // UIRotationGestureRecognizer *rotation = [UIRotationGestureRecognizer alloc]initWithTarget:<#(nullable id)#> action:<#(nullable SEL)#> //捏合手勢 // UIPinchGestureRecognizer *pinch = [UIPinchGestureRecognizer alloc]initWithTarget:<#(nullable id)#> action:<#(nullable SEL)#> //拖拽手勢 // UIPanGestureRecognizer *pan = [UIPanGestureRecognizer alloc]initWithTarget:<#(nullable id)#> action:<#(nullable SEL)#>
實際運用:
@interface ViewController ()<UIGestureRecognizerDelegate> @property (weak, nonatomic) IBOutlet UIImageView *imageView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self setUpPinch]; [self setUpRotation]; [self setUpPan]; } #pragma mark - 手勢代理方法 // 是否允許開始觸發(fā)手勢 //- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer //{ // return NO; //} // 是否允許同時支持多個手勢,默認是不支持多個手勢 // 返回yes表示支持多個手勢 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; } // 是否允許接收手指的觸摸點 //- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{ // // 獲取當前的觸摸點 // CGPoint curP = [touch locationInView:self.imageView]; // // if (curP.x < self.imageView.bounds.size.width * 0.5) { // return NO; // }else{ // return YES; // } //} #pragma mark - 點按手勢 - (void)setUpTap { // 創(chuàng)建點按手勢 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]; tap.delegate = self; [_imageView addGestureRecognizer:tap]; } - (void)tap:(UITapGestureRecognizer *)tap { NSLog(@"%s",__func__); } #pragma mark - 長按手勢 // 默認會觸發(fā)兩次 - (void)setUpLongPress { UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]; [self.imageView addGestureRecognizer:longPress]; } - (void)longPress:(UILongPressGestureRecognizer *)longPress { if (longPress.state == UIGestureRecognizerStateBegan) { NSLog(@"%s",__func__); } } #pragma mark - 輕掃 - (void)setUpSwipe { // 默認輕掃的方向是往右 UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe)]; swipe.direction = UISwipeGestureRecognizerDirectionUp; [self.imageView addGestureRecognizer:swipe]; // 如果以后想要一個控件支持多個方向的輕掃,必須創(chuàng)建多個輕掃手勢,一個輕掃手勢只支持一個方向 // 默認輕掃的方向是往右 UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe)]; swipeDown.direction = UISwipeGestureRecognizerDirectionDown; [self.imageView addGestureRecognizer:swipeDown]; } - (void)swipe { NSLog(@"%s",__func__); } #pragma mark - 旋轉(zhuǎn)手勢 - (void)setUpRotation { UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)]; rotation.delegate = self; [self.imageView addGestureRecognizer:rotation]; } // 默認傳遞的旋轉(zhuǎn)的角度都是相對于最開始的位置 - (void)rotation:(UIRotationGestureRecognizer *)rotation { self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation.rotation); // 復(fù)位 rotation.rotation = 0; // 獲取手勢旋轉(zhuǎn)的角度 NSLog(@"%f",rotation.rotation); } #pragma mark - 捏合 - (void)setUpPinch { UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)]; pinch.delegate = self; [self.imageView addGestureRecognizer:pinch]; } - (void)pinch:(UIPinchGestureRecognizer *)pinch { self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale); // 復(fù)位 pinch.scale = 1; } #pragma mark - 拖拽 - (void)setUpPan { UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)]; [self.imageView addGestureRecognizer:pan]; } - (void)pan:(UIPanGestureRecognizer *)pan { // 獲取手勢的觸摸點 // CGPoint curP = [pan locationInView:self.imageView]; // 移動視圖 // 獲取手勢的移動,也是相對于最開始的位置 CGPoint transP = [pan translationInView:self.imageView]; self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, transP.x, transP.y); // 復(fù)位 [pan setTranslation:CGPointZero inView:self.imageView]; // NSLog(@"%@",NSStringFromCGPoint(curP)); } @end
以上就是iOS觸摸事件以及手勢的相關(guān)內(nèi)容介紹,希望對大家學習iOS程序設(shè)計有所幫助。
相關(guān)文章
詳解IOS11新特性之larget title的實現(xiàn)
本篇文章主要介紹了詳解IOS11新特性之larget title的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12iOS中UIView實現(xiàn)不同方向的導(dǎo)角
這篇文章主要給大家介紹了關(guān)于iOS中UIView實現(xiàn)不同方向的導(dǎo)角的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或使用iOS具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-05-05深入講解iOS開發(fā)中應(yīng)用數(shù)據(jù)的存儲方式
這篇文章主要介紹了iOS開發(fā)中應(yīng)用數(shù)據(jù)的存儲方式,包括plistXML屬性列表和NSKeydeArchiver歸檔兩個部分,需要的朋友可以參考下2015-12-12