mac os之监听触摸板(捏合、旋转、三指)
文章目录
- 前言
- 一、创建工程实例
- 二、监听事件
- 1.捏合事件
- 步骤一
- 步骤二
- 2.捏合事件,以中心为坐标
- 步骤一
- 步骤二
- 3.监听旋转手势
- 步骤一
- 步骤二
- 4.滑动手势
- 步骤一
- 步骤二
- 步骤三
前言
在ios下监听触摸事件网上教程很多,但是发现并没有mac下监听触摸板事件的文章,这个举个例子,特记录一下如何在mac下进行触摸板事件监听。
注意:
1.需要Mac OS X Snow Leopard 10.6以上
2.设置正确的首选项以在“触控板系统首选项”下启用手势支持
3.进行mac开发应该已经有Cocoa包
一、创建工程实例
在 Xcode 项目中,右键单击Classes文件夹并选择Add…,然后选择New File…在MacOS X部分下,选择Cocoa Class,然后选择Objective-C class。最后,在弹出的子类中选择NSView。将新文件命名为 MyView.m
双击Xcode 项目中的MainMenu.xib文件。从 Interface Builders Library 面板中,将自定义视图拖到应用程序窗口中。
从 Interface Builders Inspector 的面板中,选择Identity选项卡并将Class弹出窗口设置为MyView。
从文件菜单中选择保存以保存您所做的更改。
在MyView类实现的drawRect:方法中添加如下代码:
NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:[self bounds] xRadius:8.0 yRadius:8.0];[path setClip];[[NSColor whiteColor] setFill];
[NSBezierPath fillRect:[self bounds]]; [path setLineWidth:3.0]; [[NSColor grayColor] setStroke];
[path stroke];
二、监听事件
1.捏合事件
步骤一
在MyView类实现的drawRect:方法中添加如下代码:
NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:[self bounds] xRadius:8.0 yRadius:8.0];[path setClip];[[NSColor whiteColor] setFill];
[NSBezierPath fillRect:[self bounds]]; [path setLineWidth:3.0]; [[NSColor grayColor] setStroke];
[path stroke];
步骤二
监听捏合事件
– (void)magnifyWithEvent:(NSEvent *)event {NSSize size = [self frame].size;
size.height = size.height * ([event magnification] + 1.0);
size.width = size.width * ([event magnification] + 1.0);[self setFrameSize:size];
}
2.捏合事件,以中心为坐标
步骤一
在MyView类实现的drawRect:方法中添加如下代码:
NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:[self bounds] xRadius:8.0 yRadius:8.0];[path setClip];[[NSColor whiteColor] setFill];
[NSBezierPath fillRect:[self bounds]]; [path setLineWidth:3.0]; [[NSColor grayColor] setStroke];
[path stroke];
步骤二
监听捏合事件,视图从中心放大和缩小
NSSize size = [self frame].size;NSSize originalSize = size;size.height = size.height * ([event magnification] + 1.0);size.width = size.width * ([event magnification] + 1.0);[self setFrameSize:size];
CGFloat deltaX = (originalSize.width – size.width) / 2;
CGFloat deltaY = (originalSize.height – size.height) / 2;NSPoint origin = self.frame.origin;
origin.x = origin.x + deltaX;
origin.y = origin.y + deltaY;
[self setFrameOrigin:origin];
3.监听旋转手势
步骤一
在MyView类实现的drawRect:方法中添加如下代码:
NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:[self bounds] xRadius:8.0 yRadius:8.0];[path setClip];[[NSColor whiteColor] setFill];
[NSBezierPath fillRect:[self bounds]]; [path setLineWidth:3.0]; [[NSColor grayColor] setStroke];
[path stroke];
步骤二
– (void)rotateWithEvent:(NSEvent *)event {CGFloat currentRotation = [self frameCenterRotation];[self setFrameCenterRotation:(currentRotation + [eventrotation])];}
4.滑动手势
当三个或更多手指在触控板上移动时,会检测到滑动手势。此手势通常用于翻阅一系列图像。在这个秘籍中,我们将创建一个自定义视图,该视图解释四个不同方向的滑动手势,并在我们的自定义视图中显示滑动方向:
步骤一
在Xcode中,打开MyView.h文件,在类接口中添加方向变量:
NSString *方向;
步骤二
打开MyView.m文件,在MyView类实现的drawRect:方法中加入如下代码:
NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:[self bounds] xRadius:8.0 yRadius:8.0];[path setClip];[[NSColor whiteColor] setFill];
[NSBezierPath fillRect:[self bounds]];
[path setLineWidth:3.0]; [[NSColor grayColor] setStroke];
[path stroke];
if (direction == nil) {direction = @””;
}NSAttributedString *string = [[[NSAttributedString alloc] initWithString:direction] autorelease];
NSPoint point = NSMakePoint(([self bounds].size.width / 2) –
([string size].width / 2), ([self bounds].size.height / 2) –
([string size].height / 2));
[string drawAtPoint:point];
步骤三
添加以下代码来处理滑动手势:
– (void)swipeWithEvent:(NSEvent *)event {if ([event deltaX] > 0) {direction = @”Left”;} else if ([event deltaX] < 0) {direction = @”Right”;} else if ([event deltaY] > 0) {direction = @”Up”;} else if ([event deltaY] < 0){direction = @”Down”;}[self setNeedsDisplay:YES];}
mac os之监听触摸板(捏合、旋转、三指)相关推荐
- linux触摸板设置密码程序6,Touchegg:Linux上触摸板/屏的多指手势
笔者在 Linux 桌面环境上使用 Touchegg 实现了接近 Mac OS X 上的工作区切换手势.这个功能很实用,因为之前体验过Mac OS X上的触摸板,对触摸板的工作区切换手势可谓印象深刻. ...
- linux触摸板设置密码程序6,Touchégg: Linux 上触摸板/屏的多指手势
By - 3月, 10th 2016 笔者之前体验过Mac OS X上的触摸板,对触摸板的工作区切换手势可谓印象深刻.而笔者所使用的笔记本搭载了支持5只手指的触摸板,因此很希望能够在此之上实现3只手指 ...
- Linux的触屏手势软件安装,Touchégg: Linux 上触摸板/屏的多指手势
笔者之前体验过Mac OS X上的触摸板,对触摸板的工作区切换手势可谓印象深刻.而笔者所使用的笔记本搭载了支持5只手指的触摸板,因此很希望能够在此之上实现3只手指以上的手势识别和交互. 这样,一款叫 ...
- flutter复制口令返回app监听粘贴板,类似App助力
@overridevoid didChangeAppLifecycleState(AppLifecycleState state) {switch (state) {case AppLifecycle ...
- Android屏幕触摸监听,Android在Fragment中实现监听触摸事件
本文给大家介绍的是监听Fragment的触摸事件实现.如果大家有更好的机制,可以留言交流,下面来看看详细的介绍: 大家都知道,我们的activity中有onTouchEvent方法,可以用来实现触摸事 ...
- Android App监听软键盘按键的三种方式与改变软键盘右下角确定键样式
Android App监听软键盘按键的三种方式与改变软键盘右下角确定键样式 actionNone : 回车键,按下后光标到下一行 actionGo : Go, actionSearch : 放大镜 a ...
- Android App监听软键盘按键的三种方式(转)
最近有类似需求,在csdn上刚好发现,粘贴过来,以防止忘记喽 前言: 我们在android手机上面有时候会遇到监听手机软键盘按键的时候,例如:我们在浏览器输入url完毕后可以点击软键盘右下角的&quo ...
- Android App监听软键盘按键的三种方式
前言: 我们在android手机上面有时候会遇到监听手机软键盘按键的时候,例如:我们在浏览器输入url完毕后可以点击软键盘右下角的"GO"按键加载url页面:在点击搜索框的时候,点 ...
- 键盘-App监听软键盘按键的三种方式
前言: 我们在android手机上面有时候会遇到监听手机软键盘按键的时候,例如:我们在浏览器输入url完毕后可以点击软键盘右下角的"GO"按键加载url页面:在点击搜索框的时候,点 ...
最新文章
- 多进程实现生产者消费者
- C# 系统服务 删除后禁用 ,删除不彻底;删除后无法重装问题解决方案
- CS224n —— lecture2的重难点讲解
- MongoDB(window安装及服务配置)
- maven项目没有src/test/java和src/test/resources目录问题解决
- 文件夹复制删除 C#
- Atitit sumdoc ta index list atiitt 2008 diary 大事记v2 s222.docx Atiti. 2010---2016大事记 just world new
- 对Retinex算法的一些理解
- dtcms php,[.net学习]dtcms从前台Flash XSS到后台Getshell
- 对与连连看求解算法的研究
- 牛客竞赛语法入门班选择结构习题C++版本参考代码及部分解析
- 观察 :人工智能与伦理道德
- 机器学习(1)——周志华
- 电商全平台订单管理OMS系统
- vue js樱花飘落背景特效
- 速码工具箱,Excel批量生成二维码,瞬间搞定!
- 自动化工具之SelendroidRobotium
- Mixins在组件中的使用方法
- 2023mathorcupA题量子计算机在信用评分卡组合优化中的应用思路
- C++头文件(xxx.h)与源文件(xxx.cpp)的关系