iOS开发之手势gesture详解

 更新时间:2016年11月23日 10:59:34   作者:ForrestWoo  
本篇文章介绍了iOS开发之手势gesture,现在分享给大家,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。

前言  

在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];

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

相关文章

  • iOS App使用SQLite之句柄的定义及数据库的基本操作

    iOS App使用SQLite之句柄的定义及数据库的基本操作

    SQLite中在定义过句柄之后就可以指向数据库,从而利用iOS应用程序进行打开或关闭等各种操作,这里我们就来看一下iOS App使用SQLite之句柄的定义及数据库的基本操作
    2016-06-06
  • 苹果公司推出的新编程语言Swift简介和入门教程

    苹果公司推出的新编程语言Swift简介和入门教程

    这篇文章主要介绍了苹果公司推出的新编程语言Swift简介和入门教程,Swift是苹果于WWDC 2014.6.3发布的编程语言,主要用来替代Objective-C,需要的朋友可以参考下
    2014-06-06
  • iOS开发中苹果输入手机号变用户的名字

    iOS开发中苹果输入手机号变用户的名字

    今天我们的用户输入手机号之后变成了用户的名字,没办法获取验证码,因为手机格式不对。下面通过本文给大家分享开发中苹果输入手机号变用户的名字,需要的朋友可以参考下
    2017-05-05
  • 关于iOS自带九宫格拼音键盘和Emoji表情之间的一些坑

    关于iOS自带九宫格拼音键盘和Emoji表情之间的一些坑

    这篇文章主要给大家介绍了关于iOS自带九宫格拼音键盘和Emoji表情之间的一些坑文中通过示例代码介绍的非常详细,对各位iOS开发者们具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-05-05
  • iOS中利用CoreAnimation实现一个时间的进度条效果

    iOS中利用CoreAnimation实现一个时间的进度条效果

    在iOS中实现进度条通常都是通过不停的设置progress来完成的,这样的进度条适用于网络加载(上传下载文件、图片等)。下面通过本文给大家介绍iOS中利用CoreAnimation实现一个时间的进度条,需要的的朋友参考下吧
    2017-09-09
  • Navigation bar的注意事项详解

    Navigation bar的注意事项详解

    本文主要介绍了Navigation bar的注意事项。具有一定的参考价值,下面跟着小编一起来看下吧
    2017-01-01
  • iOS 生成图片验证码绘制实例代码

    iOS 生成图片验证码绘制实例代码

    本篇文章主要介绍了iOS 图片验证码绘制实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • iOS中字符串换行的实现方法

    iOS中字符串换行的实现方法

    大家应该都有所体会,单行字符数过多会影响美观,所以下面这篇文章主要给大家介绍了关于iOS中字符串换行的实现方法,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2018-07-07
  • IOS开发代码分享之设置UISearchBar的背景颜色

    IOS开发代码分享之设置UISearchBar的背景颜色

    在项目开发中,我们经常要用到UISearchBar,在网上看到了很多关于去除掉他背景色的方法,都已经失效了,今天来分享一个正常使用的方法,希望能帮到大家
    2014-09-09
  • IOS代码笔记之下拉选项cell

    IOS代码笔记之下拉选项cell

    这篇文章主要为大家详细介绍了IOS代码笔记之下拉选项cell的相关资料,需要的朋友可以参考下
    2016-07-07

最新评论