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. 重新安装python

    1. 在上次进行安装python的时候,很多东西不能用,例如后退键,删除键,都是不能在命令行中使用,主要原因是在编译python的时候,相关的安装包没有进行安装,从而导致出现乱码,在编译最新版本的py ...

  2. python 映射列表 学习

    列表映射是个非常有用的方法,通过对列表的每个元素应用一个函数来转换数据,可以使用一种策略或者方法来遍历计算每个元素. 例如: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...

  3. Ubuntu上用快捷键关闭没有响应的程序

    Linux 上有很多方法可以强制关闭无响应的程序,比如你可以通过按快捷键 Ctrl + Shift + T 来调出 Terminal 或者用 Ctrl + Shift + F1 进入 Console ...

  4. dedecms list 判断 每隔3次输出内容

    {dede:list pagesize='12' runphp='yes'} [field:global name=autoindex runphp="yes"](@me%3==0 ...

  5. HttpClient 操作总结

    1.HttpClient4.3和之前版本设置超时(set timeout)区别: 参考:http://my.oschina.net/u/577453/blog/173724 解析:如果不设置超时的话, ...

  6. 第三百四十八天 how can I 坚持

    回来的倒不晚,算了不想抱怨了. 晚上回来吃过饭,又看了遍<活着>,把一切悲剧都放在一个人身上了,很朴实,好感人. 一天就写了一个借口,也是醉了. 我的天气预报,我的struts.sprin ...

  7. UVALive 3956 Key Task (bfs+状态压缩)

    Key Task 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/D Description The Czech Technica ...

  8. C# rmi例子

    接口定义 实体定义,注意需要序列化 using System; namespace Interface { [Serializable] public class DataEntity { publi ...

  9. RGPJS 教程之八 创造场景

    开始画面 游戏画面 代码 <!DOCTYPE html> <html> <head> <script src="rpg-beta-2.js" ...

  10. UVaLive 6694 Toy Boxes (二分+想法)

    题意:给出n个数,把n个数放在三个盒子里,每个盒子里的数绑在一起,要拿出来任何一个数的时候,所承担的重量是整个盒子的总重量,求最小总重量和. 析:感觉吧,就是轻的放的多一些,拿的次数多一些,大的放的少 ...