UIKit中包含了UIGestureRecognizer类,用于检测发生在设备中的手势。UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,它有下面一些子类用于处理具体的手势:

1、拍击UITapGestureRecognizer (任意次数的拍击)

2、向里或向外捏UIPinchGestureRecognizer (用于缩放)

3、摇动或者拖拽UIPanGestureRecognizer

4、擦碰UISwipeGestureRecognizer (以任意方向)

5、旋转UIRotationGestureRecognizer (手指朝相反方向移动)

6、长按UILongPressGestureRecognizer

对 于不同类型的手势识别器,具有不同的配置属性。比如UITapGestureRecognizer,可以配置拍击次数。界面接收到手势之后,可以发送一个 消息,用于处理响应手势动作后的任务。当然,不同的手势识别器,发送的消息方法也会有所不同。下面列举几个具体示例代码:

1、一个手指,拍击两次手势

// 创建一个手势识别器


UITapGestureRecognizer *oneFingerTwoTaps = 


  [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerTwoTaps)] autorelease];

 


// Set required taps and number of touches


[oneFingerTwoTaps setNumberOfTapsRequired:2];


[oneFingerTwoTaps setNumberOfTouchesRequired:1];

 


// Add the gesture to the view


[[self view] addGestureRecognizer:oneFingerTwoTaps];

消息方法oneFingerTwoTaps


- (void)oneFingerTwoTaps


{


  NSLog(@"Action: One finger, two taps");


}

2、两个手指,拍击两次手势


UITapGestureRecognizer *twoFingersTwoTaps = 


  [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersTwoTaps)] autorelease];


[twoFingersTwoTaps setNumberOfTapsRequired:2];


[twoFingersTwoTaps setNumberOfTouchesRequired:2];


[[self view] addGestureRecognizer:twoFingersTwoTaps];

消息方法twoFingersTwoTaps


- (void)twoFingersTwoTaps {


  NSLog(@"Action: Two fingers, two taps");


}

3、一个手指向上、向下擦碰手势


// 向上擦碰


UISwipeGestureRecognizer *oneFingerSwipeUp = 


  [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeUp:)] autorelease];


[oneFingerSwipeUp setDirection:UISwipeGestureRecognizerDirectionUp];


[[self view] addGestureRecognizer:oneFingerSwipeUp];

- (void)oneFingerSwipeUp:(UISwipeGestureRecognizer *)recognizer 





  CGPoint point = [recognizer locationInView:[self view]];


  NSLog(@"Swipe up - start location: %f,%f", point.x, point.y);


}

// 向下擦碰


UISwipeGestureRecognizer *oneFingerSwipeDown = 


  [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(oneFingerSwipeDown:)] autorelease];


[oneFingerSwipeDown setDirection:UISwipeGestureRecognizerDirectionDown];


[[self view] addGestureRecognizer:oneFingerSwipeDown];

- (void)oneFingerSwipeDown:(UISwipeGestureRecognizer *)recognizer 





  CGPoint point = [recognizer locationInView:[self view]];


  NSLog(@"Swipe down - start location: %f,%f", point.x, point.y);


}

4、旋转手势


UIRotationGestureRecognizer *twoFingersRotate = 


  [[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingersRotate:)] autorelease];


[[self view] addGestureRecognizer:twoFingersRotate];

- (void)twoFingersRotate:(UIRotationGestureRecognizer *)recognizer 


{


  // Convert the radian value to show the degree of rotation


  NSLog(@"Rotation in degrees since last change: %f", [recognizer rotation] * (180 / M_PI));


}

5、向里或向外捏的手势


UIPinchGestureRecognizer *twoFingerPinch = 


  [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerPinch:)] autorelease];


[[self view] addGestureRecognizer:twoFingerPinch];

- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer 


{


  NSLog(@"Pinch scale: %f", recognizer.scale);


}

使用iOS手势UIGestureRecognizer的更多相关文章

  1. iOS手势UIGestureRecognizer的使用失效问题

    问题:视图正常展示在界面中,父层是放在window上的,底部的一个控件的点击事件失效(所有设置都正常) 解决思路:虽然视图能够正常展示,但是发现父类视图的底部尺寸比子类的视图的尺寸小,也就是说上层视图 ...

  2. iOS手势UIGestureRecognizer的使用及手势冲突的解决办法【转】

    转自:iOS开发中的手势体系——UIGestureRecognizer分析及其子类的使用 关于手势的一篇很好的帖子,转载过来免得丢失.你可能最感兴趣的是手势间的互斥处理,那么就搜索 4.手势间的互斥处 ...

  3. IOS手势UIGestureRecognizer

    UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,它有6个子类处理具体的手势: 1.UITapGestureRecognizer (任意手指任意次数的点击) // 点击次数 ...

  4. 点击事件touches与ios的手势UIGestureRecognizer

    .h文件 @property (weak,nonatomic) IBOutlet UILabel *messageLabel;@property (weak,nonatomic) IBOutlet U ...

  5. [BS-25] IOS中手势UIGestureRecognizer概述

    IOS中手势UIGestureRecognizer概述 一.概述 iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由UIResponder而来的如下4种方式: - (void)touches ...

  6. iOS手势学习UIGestureRecognizer & cocos2d 手势推荐

    iOS手势学习UIGestureRecognizer & cocos2d 手势推荐 手势识别类型: UILongPressGestureRecognizer  // 长按UIPanGestur ...

  7. ios手势

    iOS 手势操作:拖动.捏合.旋转.点按.长按.轻扫.自定义 大 中 小   1.UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性. i ...

  8. iOS手势处理

    iOS手势处理 iOS手势有着如下几种: UITapGestureRecognizer UIPinchGestureRecognizer UIRotationGestureRecognizer UIS ...

  9. iOS Programming UIGestureRecognizer and UIMenuController

    iOS  Programming  UIGestureRecognizer and UIMenuController A UIGestureRecognizer intercepts touches ...

随机推荐

  1. 【LeetCode】169 - Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  2. 【LeetCode】204 - Count Primes

    Description:Count the number of prime numbers less than a non-negative number, n. Hint: Let's start ...

  3. “内部类” 大总结(Java)

    (本文整理自很久以前收集的资料(我只是做了排版修改),作者小明,链接地址没有找到,总之感谢,小明) (后面也对"静态内部类"专门做了补充) 内部类的位置: 内部类可以作用在方法里以 ...

  4. WMI使用的WIN32_类库名

    WMI使用的WIN32_类库名 包括:硬件类.操作系统类.安装应用程序类.WMI服务管理类.性能计数器类1.硬件类冷却类别Win32_Fan--风扇Win32_HeatPipe--热管Win32_Re ...

  5. JavaScript高级程序设计(第三版)第五章 引用类型

    5.2 Array类型 var colors = new Array(3); //创建一个包含3项的数组 var names = new Array("Greg"); //创建一个 ...

  6. 第二百三十一天 how can I 坚持

    哎,蛋疼的一天,一点破问题搞了一下午,还没搞利索. 他们要组织出去玩,我没有参加啊,随便找了个借口. 博客园的字体怎么变小了呢,看着好难受啊,昨天传照片传的? 睡觉.外边下着雨呢,喜欢下雨的夏天还有下 ...

  7. 网上关于sort结构体排序都不完整,我来写一个完整版的 2014-08-09 16:50 60人阅读 评论(0) 收藏

    主要参考sort函数_百度文库, 但是那篇有错误 2.结构体排序,a升,b降,c降 平板视图 打印? 01 #include <iostream> 02 #include <algo ...

  8. Field 'SCHED_TIME' doesn't have a default value

    出现这个情况的原因是: 我jar包使用的是quartz-2.1.7版本,但是初始化集群的dbTables脚本用的却是2.2.1版本的,导致出现这个异常,改用2.1.7的dbTables脚本之后即解决问 ...

  9. USB -- scsi命令集

    摘自:<圈圈教你玩usb> 241页 SCSI(small computer system interface)是小型计算机系统的缩写,有一套完整的协议规定其命令和命令数据的响应.scsi ...

  10. Dell商用台式机、笔记本、服务器800电话

    戴尔Optiplex商用台式机 售后服务电话 800-858-0950 选1选2选2 戴尔Latitude商用笔记本 售后服务电话 800-858-0950 选1选3选2 戴尔服务器PowerEdge ...