首页 > 其他分享 >使用iOS手势UIGestureRecognizer(转)

使用iOS手势UIGestureRecognizer(转)

时间:2022-09-28 14:03:32浏览次数:54  
标签:UISwipeGestureRecognizer self iOS twoFingersTwoTaps recognizer UIGestureRecogniz

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

1、拍击UITapGestureRecognizer (任意次数的拍击)
2、向里或向外捏UIPinchGestureRecognizer (用于缩放)
3、摇动或者拖拽UIPanGestureRecognizer
4、擦碰UISwipeGestureRecognizer (以任意方向)
5、旋转UIRotationGestureRecognizer (手指朝相反方向移动)
6、长按UILongPressGestureRecognizer

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

// 创建一个手势识别器
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");
}
[代码] 两个手指,拍击两次手势

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");
}
[代码] 一个手指向上、向下擦碰手势

// 向上擦碰
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);
}
[代码] 旋转手势

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));
}
[代码] 向里或向外捏的手势

UIPinchGestureRecognizer *twoFingerPinch =
[[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(twoFingerPinch:)] autorelease];
[[self view] addGestureRecognizer:twoFingerPinch];- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer
{
NSLog(@"Pinch scale: %f", recognizer.scale);
}

标签:UISwipeGestureRecognizer,self,iOS,twoFingersTwoTaps,recognizer,UIGestureRecogniz
From: https://blog.51cto.com/u_13760719/5719220

相关文章

  • ios开发之app内起动用户评价 (转)
    ios开发之app内启动用户评价恰当的指引用户去给app评分是获得用户反馈的一种不错的途径。“通过customurlscheme启动应用程序”这篇文章里讲了通过url启动地图、邮件等程......
  • ios游戏发布流程
    这里假设你已经有苹果的开发者帐号了。其实早在两年前我就已经用过这个了,现在再回忆一下。因为苹果现在为开发者增加了macos的appstore发布权限,也增加了tvOS发布应用权限,......
  • vue3+vant+vue-router+axios+pinia+vite框架搭建
    vue3的官网地址:https://cn.vuejs.org/;这里要说一下,vue3不支持IE11,如果要兼容IE11及其一下,不建议使用vue3。创建vue脚手架,如果你需要使用ts,则需要node版本>=16。本文按照......
  • axios学习笔记
     一.  安装json-server 01安装npminstall-gjson-serverhttps://github.com/typicode/json-server 02,新建一个db.json文件,把上面链接文档的数据放上去......
  • iOS开发之各机型屏幕大小与键盘高度整理
    机型屏幕宽屏幕高键盘高iPhone8375667260iPhone8Plus414736271iPhoneX375812336iPhoneXs375812336iPhoneXsMax414896346iP......
  • 2022-09-27 IOS 打包后图标背景变黑 Android 则正常
    前言:uniapp项目打包app,app的logo背景是透明的,打包后iso的图标背景是黑色,而Android则为透明,符合需求,至于ios为什么是黑色的,百度了一看有以下情况:图标的纤细信息里面的一个......
  • 已解决:axios get请求不跨域,post请求跨域了
       做了个原生的简单页面,但是需要请求到后端的数据,然后它跨域了,后台开启了允许跨域,这时候get请求是正常了,post请求带参数就出现了跨域,如图:<!DOCTYPEhtml><html> ......
  • axios请求时根据情况不同服务器返回两种类型的数据,解决方法。例如:请求下载导出一个文
    我们不需要根据服务器返回的情况去随机设置responseType一、我们要明白,我们在请求下载文件的api时候,可能给我们的返回值有两种情况:直接给我们了我们想要的文件流还有可......
  • vue使用axios请求c#mvc时显示跨域错误
    解决方法如果允许整个站点对外的跨域访问,则可以在改接口的网站mvc的web.config文件中对允许跨域访问的域名进行设置,如果不指定具体域名,可在<system.webServer></system.w......
  • axios同时请求多个接口并发
    getmytask(){showFullScreenLoading()constparam={//"status":"All/Going/Plan/Delay/Finish","s......