iOS開發之觸摸事件以及手勢_IOS

來源:互聯網
上載者:User

iOS中的事件分為三類:觸摸事件、加速計事件、遠端控制事件。只有繼承了UIResponder的對象才能接收並處理事件,稱之為“響應者對象”。UIApplication、UIViewController、UIView都繼承自UIResponder。UIResponder內部提供的方法來處理事件:

觸摸事件:touchesBegan、touchesMoved、touchesEnded、touchesCancelled

加速計事件:motionBegan、motionEnded、motionCancelled

遠端控制事件:remoteControlReceivedWithEvent

UIVeiw的觸摸事件處理過程:

/** * 當手指開始觸摸view時調用 * * @param touches <#touches description#> * @param event  <#event description#> */- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {     NSLog(@"%s",__func__);} /** * 當手指在view上移動時調用 * * @param touches <#touches description#> * @param event  <#event description#> */- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {  NSLog(@"%s",__func__);} /** * 當手指離開view時調用 * * @param touches <#touches description#> * @param event  <#event description#> */- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {     NSLog(@"%s",__func__);} /** * 當觸摸事件被系統事件打斷時調用 * * @param touches <#touches description#> * @param event  <#event description#> */- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {     NSLog(@"%s",__func__);}

一次觸摸動作必然會調用touchesBeagn、touchesMoved和touchesEnded這三個方法。

說到這幾個觸摸方法,首先要知道UITouch這個對象。當一根手指觸控螢幕幕時就會產生一個與之關聯的UITouch對象,一根手指對應一個UITouch對象。這個對象裡面儲存著這次觸摸的資訊,比如觸摸的位置,時間,階段等,當手指移動時,系統會更新同一個UITouch對象。使其能一直儲存該手指所在的觸摸位置資訊。當手指離開螢幕時,系統會銷毀對應的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上移動時調用 * * @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);}


就是根據UITouch對象中儲存的位置資訊來實現的。

事件的產生和傳遞:

當觸摸事件產生後,系統會將該事件添加到一個由UIApplication管理的事件隊列中去。UIApplication會從隊列中取出最前面的事件,發送給應用程式的主視窗的處理。主視窗會在視圖階層中,找一個最合適的視圖並調用touches方法來處理觸摸事件。觸摸事件的傳遞是從父控制項傳遞到子控制項。如果父控制項不能接收到觸摸事件,那麼子控制項就不可能 接收到觸摸事件。

如何找到最合適的控制項來處理事件?首先判斷自己是否能接收觸摸事件?觸摸點是否在自己身上?從後往前遍曆子控制項,重複之前的兩個步驟,如果沒有合格子控制項,那麼就自己最合適處理。

控制項用hitTest:withEvent:方法來尋找最合適的view,用pointInside這個方法判斷這個點在不在方法調用者即控制項身上。

hitTest方法的底層實現:

- (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];         //把當前控制項上的座標系轉換成子控制項上的座標系    CGPoint childPoint = [self convertPoint:point toView:childView];         //遞迴調用hitTest方法尋找最合適的view    UIView *fitView = [childView hitTest:childPoint withEvent:event];         if (fitView) {      return fitView;    }  }     //迴圈結束,沒有比自己更合適的view,返回自己  return self;   }

然而使用touches方法監聽觸摸事件是有缺點的,比如要自訂view,所以iOS3.2之後蘋果推出了手勢識別功能UIGestureRecognizer。UIGestureRecognizer是一個抽象類別,它的子類才能處理具體的某個手勢。

具體有以下幾種手勢:

//點按手勢//  UITapGestureRecognizer *tap = [UITapGestureRecognizer alloc]initWithTarget:<#(nullable id)#> action:<#(nullable SEL)#>     //長按手勢 預設是觸發兩次//  UILongPressGestureRecognizer *longP = [UILongPressGestureRecognizer alloc]initWithTarget:<#(nullable id)#> action:<#(nullable SEL)#>     //撥動手勢 預設方向是往右//  UISwipeGestureRecognizer *swipe = [UISwipeGestureRecognizer alloc]initWithTarget:<#(nullable id)#> action:<#(nullable SEL)#>     //旋轉手勢//  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 - 手勢代理方法// 是否允許開始觸發手勢//- (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{  // 建立點按手勢  UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];     tap.delegate = self;     [_imageView addGestureRecognizer:tap];} - (void)tap:(UITapGestureRecognizer *)tap{  NSLog(@"%s",__func__);} #pragma mark - 長按手勢// 預設會觸發兩次- (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];     // 如果以後想要一個控制項支援多個方向的輕掃,必須建立多個撥動手勢,一個撥動手勢只支援一個方向  // 預設輕掃的方向是往右  UISwipeGestureRecognizer *swipeDown = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe)];     swipeDown.direction = UISwipeGestureRecognizerDirectionDown;     [self.imageView addGestureRecognizer:swipeDown];    } - (void)swipe{  NSLog(@"%s",__func__);} #pragma mark - 旋轉手勢- (void)setUpRotation{  UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];  rotation.delegate = self;  [self.imageView addGestureRecognizer:rotation];} // 預設傳遞的旋轉的角度都是相對於最開始的位置- (void)rotation:(UIRotationGestureRecognizer *)rotation{     self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation.rotation);     // 複位  rotation.rotation = 0;     // 擷取手勢旋轉的角度  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);     // 複位     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);     // 複位  [pan setTranslation:CGPointZero inView:self.imageView];    // NSLog(@"%@",NSStringFromCGPoint(curP));} @end

以上就是iOS觸摸事件以及手勢的相關內容介紹,希望對大家學習iOS程式設計有所協助。

相關文章

聯繫我們

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