iOS開發之手勢gesture詳解_IOS

來源:互聯網
上載者:User

前言  

在iOS中,你可以使用系統內建的手勢識別(GestureRecognizer),也可以建立自己的手勢.GestureRecognizer將低層級的轉換為進階別的執行行為,是你綁定到view的對象,當發生手勢,綁定到的view對象會響應,它確定這個動作是否對應一個特定的手勢(swipe,pinch,pan,rotation).如果它能識別這個手勢,那麼就會向綁定它的view發送訊息,如下圖

UIKit架構提供了一些預定義的GestureRecognizer.包含下列手勢

  •  UITapGestureRecognizer敲擊手勢(單擊和雙擊)
  •  UIPanGestureRecognizer(拖動手勢)
  •  UIPinchGestureRecognizer(縮放手勢)
  •  UISwipeGestureRecognizer(擦碰手勢)
  •  UIRotationGestureRecognizer(旋轉手勢)
  •  UILongPressGestureRecognizer(長按手勢)

如果你想讓你的應用程式來識別一個獨特的手勢,如選擇目錄或糾結的運動,你可以建立自己的自訂GestureRecognizer,將在下篇介紹

將特定的手勢和view相關聯

每一個特定的手勢必須關聯到view對象中才會有作用,一個view對象可以關聯多個不同的特定手勢,但是每一個特定的手勢只能與一個view相關聯。當使用者觸摸了view,這個GestureRecognizer就會接受到訊息,它可以響應特定的觸摸事件。

與特定view關聯

  • 建立GestureRecognizer執行個體
  • addGestureRecognizer
  • 實現處理手勢的方法

可以使用removeGestureRecognizer: 來移除手勢。

_panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlerPanGesture:)]; _panGestureRecognizer.delegate = self; _panGestureRecognizer.maximumNumberOfTouches = 2; _panGestureRecognizer.minimumNumberOfTouches = 2; [self.view addGestureRecognizer:_panGestureRecognizer];- (void)handlerPanGesture:(UIPanGestureRecognizer *)recognizer{ if ((recognizer.state == UIGestureRecognizerStateBegan) ||  (recognizer.state == UIGestureRecognizerStateChanged)) {  CGPoint offset = [recognizer translationInView:self.view];  CGRect frame = self.rightViewController.view.frame;  frame.origin.x += offset.x;  if (frame.origin.x >= 0 && frame.origin.x <= kScreenWidth)  {   self.rightViewController.view.frame = frame;  }    [recognizer setTranslation:CGPointZero inView:self.view]; } else if (recognizer.state == UIGestureRecognizerStateEnded) {  BOOL isVisible = self.rightViewController.view.frame.origin.x < kScreenWidth / 2;  [self showRightView:isVisible]; }}

手勢識別狀態
Gesture recognizers從一個狀態轉到另一狀態(state)。對於每個狀態,根據它們是否符合特定條件來決定時候可以移動到下一個狀態。它們分析多點觸摸。是否識別失敗。未能識別手勢意味著state 轉換失敗。UIGestureRecognizerStateFailed。詳見UIGestureRecognizerState枚舉

typedef NS_ENUM(NSInteger, UIGestureRecognizerState) { UIGestureRecognizerStatePossible, // the recognizer has not yet recognized its gesture, but may be evaluating touch events. this is the default state  UIGestureRecognizerStateBegan,  // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop UIGestureRecognizerStateChanged, // the recognizer has received touches recognized as a change to the gesture. the action method will be called at the next turn of the run loop UIGestureRecognizerStateEnded,  // the recognizer has received touches recognized as the end of the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible UIGestureRecognizerStateCancelled, // the recognizer has received touches resulting in the cancellation of the gesture. the action method will be called at the next turn of the run loop. the recognizer will be reset to UIGestureRecognizerStatePossible  UIGestureRecognizerStateFailed,  // the recognizer has received a touch sequence that can not be recognized as the gesture. the action method will not be called and the recognizer will be reset to UIGestureRecognizerStatePossible  // Discrete Gestures – gesture recognizers that recognize a discrete event but do not report changes (for example, a tap) do not transition through the Began and Changed states and can not fail or be cancelled UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible};

為view添加多個手勢

當一個view添加多個手勢時,在預設情況下,沒有為優先執行哪個手勢做排序,每次發生不同。不過你可以覆蓋預設的行為(使用類方法、委託方法、和子類化覆蓋這些)

指定一個Gesture recognizers應該在另一個前捕捉。

requireGestureRecognizerToFail: 這個方法就是在作為參數的Gesture recognizer失敗以後接受者才發生,否則從不會發生。

[self.panRecognizer requireGestureRecognizerToFail:self.swipeRecognizer];

允許2個手勢同時操作

gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:

禁止在某一點發生Gesture recognizers

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{ if ([touch.view isKindOfClass:[UIControl class]]) {  return NO; }  return YES;}

指定一個單向關係兩個手勢辨識器

想控制兩個辨識器相互作用,但你需要指定一個單向關係,您可以重寫或canPreventGestureRecognizer:或canBePreventedByGestureRecognizer:子類方法。return yes。例如,如果你想要一個旋轉的姿態來防止捏動作,但你不想夾手勢防止旋轉的姿態。例如,你想一個旋轉手勢阻止一個縮放手勢,但你不想一個縮放手勢阻止旋轉手勢,就加入下面代碼

[rotationGestureRecognizer canPreventGestureRecognizer:pinchGestureRecognizer];

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

相關文章

聯繫我們

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