前言  

  在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 = ;
_panGestureRecognizer.minimumNumberOfTouches = ;
[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 >= && 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 / ;
[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];

转自:http://www.cnblogs.com/salam/archive/2013/04/30/iOS_gesture.html

iOS开发之手势gesture详解(一)的更多相关文章

  1. 【转】 iOS开发之手势gesture详解

    原文:http://www.cnblogs.com/salam/archive/2013/04/30/iOS_gesture.html 前言 在iOS中,你可以使用系统内置的手势识别 (Gesture ...

  2. iOS开发之手势gesture详解(二)

    与其他用户界面控件交互 UIControl子类会覆盖parentView的gesture.例如当用户点击UIButton时,UIButton会接受触摸事件,它的parentView不会接收到.这仅适用 ...

  3. iOS开发摇动手势实现详解

    1.当设备摇动时,系统会算出加速计的值,并告知是否发生了摇动手势.系统只会运动开始和结束时通知你,并不会在运动发生的整个过程中始终向你报告每一次运动.例如,你快速摇动设备三次,那只会收到一个摇动事件. ...

  4. iOS 开发之照片框架详解(2)

    一. 概况 本文接着 iOS 开发之照片框架详解,侧重介绍在前文中简单介绍过的 PhotoKit 及其与 ALAssetLibrary 的差异,以及如何基于 PhotoKit 与 AlAssetLib ...

  5. iOS 开发之照片框架详解之二 —— PhotoKit 详解(下)

    本文链接:http://kayosite.com/ios-development-and-detail-of-photo-framework-part-three.html 这里接着前文<iOS ...

  6. iOS 开发之照片框架详解

    转载自:http://kayosite.com/ios-development-and-detail-of-photo-framework.html 一. 概要 在 iOS 设备中,照片和视频是相当重 ...

  7. iOS 开发之照片框架详解之二 —— PhotoKit 详解(上)

    转载自:http://kayosite.com/ios-development-and-detail-of-photo-framework-part-two.html 一. 概况 本文接着 iOS 开 ...

  8. iOS开发——UI篇&ScrollView详解

    创建方式 1:StoryBoard/Xib 这里StoarBoard就不多说,直接拖就可以,说太多没意思,如果连这个都不会我只能先给你跪了! 2:代码: CGRect bounds = [ [ UIS ...

  9. IOS开发中单例模式使用详解

    第一.基本概念 单例模式是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例类的特殊类.通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问. 第二.在IOS中使用单例模式的情 ...

随机推荐

  1. HDU-1072-Nightmares

    这题可以用dfs写,我们记忆化搜索. 我们定义一个step和time数组,分别表示走到这点的最小步数,time表示走到该点炸弹还剩多少时间. 递归边界一是,如果走到该点,时间等于0,我们就返回. 如果 ...

  2. html5新结构标签

    html5新结构标签 <header>  定义 section 或 page 的页眉,也就是定义头部的标签. <footer> 定义 section 或 page 的页脚. & ...

  3. Choose unique values for the 'webAppRootKey' context-param in your web.xml files! 错误的解决

    大意是Log4jConfigListener在获取webapp.root值时,被后一context的值替换掉了,所以要在各个项目的web.xml中配置不同的webAppRootKey值,随即在其中一个 ...

  4. spring事务(Transaction )报 marked as rollback-only异常的原因及解决方法

    很多朋友在使用spring+hibernate或mybatis等框架时经常遇到报Transaction rolled back because it has been marked as rollba ...

  5. Codeforces Round #505 D. Recovering BST(区间DP)

    首先膜一发网上的题解.大佬们tql. 给你n个单调递增的数字,问是否能够把这些数字重新构成一棵二叉搜索树(BST),且所有的父亲结点和叶子结点之间的gcd > 1? 这个题场上是想暴力试试的.结 ...

  6. poj 3262 牛毁坏花问题 贪心算法

    题意:有n头牛,每头牛回去都需要一定时间,如果呆在原地就会毁坏花朵.问:怎么安排使得毁坏的花朵最少? 思路: 拉走成本最高的. 什么是成本?毁坏花朵的数量. 例如有两种排序   (这里用(a,b)表示 ...

  7. UVa 10723 LCS变形 Cyborg Genes

    题解转自: UVA 10723 Cyborg Genes - Staginner - 博客园 首先这个题目肯定是按最长公共子序列的形式进行dp的,因为只有保证消去的一部分是最长公共子序列才能保证最后生 ...

  8. Python虚拟机函数机制之位置参数的默认值(五)

    位置参数的默认值 在Python中,允许函数的参数有默认值.假如函数f的参数value的默认值是1,在我们调用函数时,如果传递了value参数,那么f调用时value的值即为我们传递的值,如果调用时没 ...

  9. 请求报文&响应报文

    转自黑马程序员视频教程

  10. webdriver高级应用- 高亮显示正在操作的页面元素

    #encoding=utf-8 import unittest from selenium import webdriver import time def highLightElement(driv ...