博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
手势UIGestureRecognizer
阅读量:5156 次
发布时间:2019-06-13

本文共 3114 字,大约阅读时间需要 10 分钟。

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);}

转载于:https://www.cnblogs.com/q403154749/p/3928097.html

你可能感兴趣的文章
Program exited with code **** 相关解释
查看>>
植物大战僵尸中文年度版
查看>>
26、linux 几个C函数,nanosleep,lstat,unlink
查看>>
投标项目的脚本练习2
查看>>
201521123107 《Java程序设计》第9周学习总结
查看>>
Caroline--chochukmo
查看>>
iOS之文本属性Attributes的使用
查看>>
从.Net版本演变看String和StringBuilder性能之争
查看>>
Excel操作 Microsoft.Office.Interop.Excel.dll的使用
查看>>
解决Ubuntu下博通网卡驱动问题
查看>>
【bzoj2788】Festival
查看>>
执行gem install dryrun错误
查看>>
Java SE之正则表达式一:概述
查看>>
HTML5简单入门系列(四)
查看>>
实现字符串反转
查看>>
转载:《TypeScript 中文入门教程》 5、命名空间和模块
查看>>
苹果开发中常用英语单词
查看>>
[USACO 1.4.3]等差数列
查看>>
Shader Overview
查看>>
Reveal 配置与使用
查看>>