iPhone开发进阶(8)--- 检测屏幕触摸事件
来源:互联网 发布:电脑的mac地址怎么查看 编辑:程序博客网 时间:2024/06/05 12:38
这一回来定制 UIView 上的触摸事件,作为例子,只是简单地检测出触摸事件并显示当前坐标在控制台上。
首先添加新文件,如下图:
在显示的对话框中选中 Cocoa Touch Class 的 Objective C class ⇒ UIView
在项目的添加菜单中选择 Touch 。检测触摸时间需要实现下面的函数。
12
- (void)touchesBegan:(NSSet *)toucheswithEvent:(UIEvent *)event;
这个函数由用户触摸屏幕以后立刻被调到。为了自定义他的行为,我们像下面来实现:
12345
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch* touch = [touches anyObject]; CGPoint pt = [touch locationInView:self]; printf("point = %lf,%lf/n", pt.x, pt.y);}
上面的代码将触摸点的坐标取出,并打印到控制台上。
如果需要得到多点触摸(不只是一根手指)的信息,需要使用 anyObject 实例指定 UIView。
另外,TouchAppDelegate 的 applicationDidFinishLaunching 函数像下面一样实现:
12345678
- (void)applicationDidFinishLaunching:(UIApplication *)application { TouchView* view = [[TouchView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)]; view.backgroundColor = [UIColor greenColor]; [window addSubview:view]; [window makeKeyAndVisible]; [view release];}
这里用 intiWithFrame 指定的矩形区域可以任意。另外为了明确触摸的区域大小,设定其 view.backgroundColor。
虽然通过 initWithFrame 在 TouchAppDelegate 内创建了 TouchView 的实例、但是通过 addSubview:view 将管理责任交给了 window 。就是说, TouchAppDelegate 与 window 两个实例都对 TouchView 实例实施管理。所以这里用 [view release] 释放 TouchAppDelegate 的管理责任。
- iPhone开发进阶(8)— 检测屏幕触摸事件
- iPhone开发进阶(8)--- 检测屏幕触摸事件
- iphone 检测屏幕触摸事件
- 检测触摸事件(用户触摸屏幕时提示信息)
- unity3d 屏幕触摸检测事件
- iphone应用程序开发指南--触摸事件(笔记 3 )
- iphone开发之触摸事件详解
- iphone应用程序开发指南--触摸事件
- ios 进阶 -- 触摸事件
- android屏幕触摸事件机制(转)
- Unity 屏幕触摸事件
- 屏幕触摸事件
- 屏幕触摸事件
- Unity 屏幕触摸事件
- 屏幕触摸事件传递
- 屏幕触摸事件
- 移动开发:iphone开发之触摸事件详解
- Cocos2d—X游戏开发之CCTouch屏幕触摸事件获取(九)
- x509数字证书介绍
- Spinner的常用技巧
- C++必知必会 - 指针比较的含义
- Linux平台静态库、动态库的一些笔记
- linux下安装kscope1.9.4.tar.gz出现的错误解决办法
- iPhone开发进阶(8)--- 检测屏幕触摸事件
- Android 核心分析 之七------Service深入分析
- iPhone开发进阶(9)--- 用SQLite管理数据库
- 基于单文档应用程序背景的修改
- Boost库安装(Vs2008环境)
- JFreeChart生成柱状图
- Android 核心分析 之八------Android 启动过程详解
- 微软以85亿美元 购买Skype
- windows操作系统中安装驱动文件时的关于数字签名的知识