iOS開(kāi)發(fā)之觸摸事件以及手勢(shì)
iOS中的事件分為三類(lèi):觸摸事件、加速計(jì)事件、遠(yuǎn)程控制事件。只有繼承了UIResponder的對(duì)象才能接收并處理事件,稱(chēng)之為“響應(yīng)者對(duì)象”。UIApplication、UIViewController、UIView都繼承自UIResponder。UIResponder內(nèi)部提供的方法來(lái)處理事件:
觸摸事件:touchesBegan、touchesMoved、touchesEnded、touchesCancelled
加速計(jì)事件:motionBegan、motionEnded、motionCancelled
遠(yuǎn)程控制事件:remoteControlReceivedWithEvent
UIVeiw的觸摸事件處理過(guò)程:
/** * 當(dāng)手指開(kāi)始觸摸view時(shí)調(diào)用 * * @param touches <#touches description#> * @param event <#event description#> */ - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSLog(@"%s",__func__); } /** * 當(dāng)手指在view上移動(dòng)時(shí)調(diào)用 * * @param touches <#touches description#> * @param event <#event description#> */ - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSLog(@"%s",__func__); } /** * 當(dāng)手指離開(kāi)view時(shí)調(diào)用 * * @param touches <#touches description#> * @param event <#event description#> */ - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSLog(@"%s",__func__); } /** * 當(dāng)觸摸事件被系統(tǒng)事件打斷時(shí)調(diào)用 * * @param touches <#touches description#> * @param event <#event description#> */ - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSLog(@"%s",__func__); }
一次觸摸動(dòng)作必然會(huì)調(diào)用touchesBeagn、touchesMoved和touchesEnded這三個(gè)方法。
說(shuō)到這幾個(gè)觸摸方法,首先要知道UITouch這個(gè)對(duì)象。當(dāng)一根手指觸摸屏幕時(shí)就會(huì)產(chǎn)生一個(gè)與之關(guān)聯(lián)的UITouch對(duì)象,一根手指對(duì)應(yīng)一個(gè)UITouch對(duì)象。這個(gè)對(duì)象里面保存著這次觸摸的信息,比如觸摸的位置,時(shí)間,階段等,當(dāng)手指移動(dòng)時(shí),系統(tǒng)會(huì)更新同一個(gè)UITouch對(duì)象。使其能一直保存該手指所在的觸摸位置信息。當(dāng)手指離開(kāi)屏幕時(shí),系統(tǒng)會(huì)銷(xiāo)毀對(duì)應(yīng)的UITouch對(duì)象。
@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); //獲取當(dāng)前位置 - (CGPoint)locationInView:(nullable UIView *)view; //獲取上一個(gè)觸摸點(diǎn)的位置 - (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:讓一個(gè)view隨著手指的移動(dòng)而移動(dòng)
/** * 當(dāng)手指在view上移動(dòng)時(shí)調(diào)用 * * @param touches <#touches description#> * @param event <#event description#> */ - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { NSLog(@"%s",__func__); //獲取UITouch對(duì)象 UITouch *touch = [touches anyObject]; //獲取當(dāng)前點(diǎn)的位置 CGPoint curP = [touch locationInView:self]; //獲取上一個(gè)點(diǎn)的位置 CGPoint preP = [touch previousLocationInView:self]; //計(jì)算x的偏移量 CGFloat offsetX = curP.x - preP.x; //計(jì)算y的偏移量 CGFloat offsetY = curP.y = preP.y; //修改view的位置 self.transform = CGAffineTransformTranslate(self.transform, offsetX, offsetY); }
就是根據(jù)UITouch對(duì)象中保存的位置信息來(lái)實(shí)現(xiàn)的。
事件的產(chǎn)生和傳遞:
當(dāng)觸摸事件產(chǎn)生后,系統(tǒng)會(huì)將該事件添加到一個(gè)由UIApplication管理的事件隊(duì)列中去。UIApplication會(huì)從隊(duì)列中取出最前面的事件,發(fā)送給應(yīng)用程序的主窗口的處理。主窗口會(huì)在視圖層次結(jié)構(gòu)中,找一個(gè)最合適的視圖并調(diào)用touches方法來(lái)處理觸摸事件。觸摸事件的傳遞是從父控件傳遞到子控件。如果父控件不能接收到觸摸事件,那么子控件就不可能 接收到觸摸事件。
如何找到最合適的控件來(lái)處理事件?首先判斷自己是否能接收觸摸事件?觸摸點(diǎn)是否在自己身上?從后往前遍歷子控件,重復(fù)之前的兩個(gè)步驟,如果沒(méi)有符合條件的子控件,那么就自己最合適處理。
控件用hitTest:withEvent:方法來(lái)尋找最合適的view,用pointInside這個(gè)方法判斷這個(gè)點(diǎn)在不在方法調(diào)用者即控件身上。
hitTest方法的底層實(shí)現(xiàn):
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { //判斷當(dāng)前控件是否能接收觸摸事件 if (self.userInteractionEnabled == NO || self.hidden == YES || self.alpha <= 0.01) { return nil; } //判斷觸摸點(diǎn)是否在當(dāng)前控件上 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]; //把當(dāng)前控件上的坐標(biāo)系轉(zhuǎn)換成子控件上的坐標(biāo)系 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é)束,沒(méi)有比自己更合適的view,返回自己 return self; }
然而使用touches方法監(jiān)聽(tīng)觸摸事件是有缺點(diǎn)的,比如要自定義view,所以iOS3.2之后蘋(píng)果推出了手勢(shì)識(shí)別功能UIGestureRecognizer。UIGestureRecognizer是一個(gè)抽象類(lèi),它的子類(lèi)才能處理具體的某個(gè)手勢(shì)。
具體有以下幾種手勢(shì):
//點(diǎn)按手勢(shì) // UITapGestureRecognizer *tap = [UITapGestureRecognizer alloc]initWithTarget:<#(nullable id)#> action:<#(nullable SEL)#> //長(zhǎng)按手勢(shì) 默認(rèn)是觸發(fā)兩次 // UILongPressGestureRecognizer *longP = [UILongPressGestureRecognizer alloc]initWithTarget:<#(nullable id)#> action:<#(nullable SEL)#> //輕掃手勢(shì) 默認(rèn)方向是往右 // UISwipeGestureRecognizer *swipe = [UISwipeGestureRecognizer alloc]initWithTarget:<#(nullable id)#> action:<#(nullable SEL)#> //旋轉(zhuǎn)手勢(shì) // UIRotationGestureRecognizer *rotation = [UIRotationGestureRecognizer alloc]initWithTarget:<#(nullable id)#> action:<#(nullable SEL)#> //捏合手勢(shì) // UIPinchGestureRecognizer *pinch = [UIPinchGestureRecognizer alloc]initWithTarget:<#(nullable id)#> action:<#(nullable SEL)#> //拖拽手勢(shì) // UIPanGestureRecognizer *pan = [UIPanGestureRecognizer alloc]initWithTarget:<#(nullable id)#> action:<#(nullable SEL)#>
實(shí)際運(yùn)用:
@interface ViewController ()<UIGestureRecognizerDelegate> @property (weak, nonatomic) IBOutlet UIImageView *imageView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self setUpPinch]; [self setUpRotation]; [self setUpPan]; } #pragma mark - 手勢(shì)代理方法 // 是否允許開(kāi)始觸發(fā)手勢(shì) //- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer //{ // return NO; //} // 是否允許同時(shí)支持多個(gè)手勢(shì),默認(rèn)是不支持多個(gè)手勢(shì) // 返回yes表示支持多個(gè)手勢(shì) - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; } // 是否允許接收手指的觸摸點(diǎn) //- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{ // // 獲取當(dāng)前的觸摸點(diǎn) // CGPoint curP = [touch locationInView:self.imageView]; // // if (curP.x < self.imageView.bounds.size.width * 0.5) { // return NO; // }else{ // return YES; // } //} #pragma mark - 點(diǎn)按手勢(shì) - (void)setUpTap { // 創(chuàng)建點(diǎn)按手勢(shì) UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]; tap.delegate = self; [_imageView addGestureRecognizer:tap]; } - (void)tap:(UITapGestureRecognizer *)tap { NSLog(@"%s",__func__); } #pragma mark - 長(zhǎng)按手勢(shì) // 默認(rèn)會(huì)觸發(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 { // 默認(rèn)輕掃的方向是往右 UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe)]; swipe.direction = UISwipeGestureRecognizerDirectionUp; [self.imageView addGestureRecognizer:swipe]; // 如果以后想要一個(gè)控件支持多個(gè)方向的輕掃,必須創(chuàng)建多個(gè)輕掃手勢(shì),一個(gè)輕掃手勢(shì)只支持一個(gè)方向 // 默認(rèn)輕掃的方向是往右 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)手勢(shì) - (void)setUpRotation { UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)]; rotation.delegate = self; [self.imageView addGestureRecognizer:rotation]; } // 默認(rèn)傳遞的旋轉(zhuǎn)的角度都是相對(duì)于最開(kāi)始的位置 - (void)rotation:(UIRotationGestureRecognizer *)rotation { self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation.rotation); // 復(fù)位 rotation.rotation = 0; // 獲取手勢(shì)旋轉(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 { // 獲取手勢(shì)的觸摸點(diǎn) // CGPoint curP = [pan locationInView:self.imageView]; // 移動(dòng)視圖 // 獲取手勢(shì)的移動(dòng),也是相對(duì)于最開(kāi)始的位置 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觸摸事件以及手勢(shì)的相關(guān)內(nèi)容介紹,希望對(duì)大家學(xué)習(xí)iOS程序設(shè)計(jì)有所幫助。
相關(guān)文章
IOS 中 new 和 alloc init 的對(duì)比
這篇文章主要介紹了IOS 中 new 和 alloc init 的區(qū)別的相關(guān)資料,需要的朋友可以參考下2017-02-02詳解IOS11新特性之larget title的實(shí)現(xiàn)
本篇文章主要介紹了詳解IOS11新特性之larget title的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12iOS中UIView實(shí)現(xiàn)不同方向的導(dǎo)角
這篇文章主要給大家介紹了關(guān)于iOS中UIView實(shí)現(xiàn)不同方向的導(dǎo)角的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或使用iOS具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05iOS開(kāi)發(fā)教程之WKWebView與JS的交互
這篇文章主要給大家介紹了關(guān)于iOS開(kāi)發(fā)教程之WKWebView與JS的交互的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位iOS開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07深入講解iOS開(kāi)發(fā)中應(yīng)用數(shù)據(jù)的存儲(chǔ)方式
這篇文章主要介紹了iOS開(kāi)發(fā)中應(yīng)用數(shù)據(jù)的存儲(chǔ)方式,包括plistXML屬性列表和NSKeydeArchiver歸檔兩個(gè)部分,需要的朋友可以參考下2015-12-12iOS在固定的label上動(dòng)態(tài)顯示所有文字
這篇文章給大家主要介紹了iOS中如何實(shí)現(xiàn),在固定的label上動(dòng)態(tài)顯示所有文字的方法,文中給出了示例和思路,對(duì)大家的理解很有幫助,感興趣的朋友們下面來(lái)一起看看吧。2016-10-10