欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS开发之手势gesture详解

程序员文章站 2024-02-15 09:21:10
前言   在ios中,你可以使用系统内置的手势识别(gesturerecognizer),也可以创建自己的手势.gesturerecognizer将低级别的转换为高级别的...

前言  

在ios中,你可以使用系统内置的手势识别(gesturerecognizer),也可以创建自己的手势.gesturerecognizer将低级别的转换为高级别的执行行为,是你绑定到view的对象,当发生手势,绑定到的view对象会响应,它确定这个动作是否对应一个特定的手势(swipe,pinch,pan,rotation).如果它能识别这个手势,那么就会向绑定它的view发送消息,如下图
iOS开发之手势gesture详解

uikit框架提供了一些预定义的gesturerecognizer.包含下列手势

  •  uitapgesturerecognizer敲击手势(单击和双击)
  •  uipangesturerecognizer(拖动手势)
  •  uipinchgesturerecognizer(缩放手势)
  •  uiswipegesturerecognizer(擦碰手势)
  •  uirotationgesturerecognizer(旋转手势)
  •  uilongpressgesturerecognizer(长按手势)

如果你想让你的应用程序来识别一个独特的手势,如选择目录或纠结的运动,你可以创建自己的自定义gesturerecognizer,将在下篇介绍

将特定的手势和view相关联

每一个特定的手势必须关联到view对象中才会有作用,一个view对象可以关联多个不同的特定手势,但是每一个特定的手势只能与一个view相关联。当用户触摸了view,这个gesturerecognizer就会接受到消息,它可以响应特定的触摸事件。iOS开发之手势gesture详解iOS开发之手势gesture详解

与特定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];
 }
}

手势识别状态iOS开发之手势gesture详解
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];

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。