iPhone 触摸时UITouch的常用方法及属性

本文详细介绍了UIKit中处理用户触摸事件的四个核心方法:touchesBegan、touchesMoved、touchesEnded 和 touchesCancelled,并解释了它们的工作原理及用途。此外还介绍了如何利用 UITouch 对象获取触摸的相关信息,如点击次数、时间戳、坐标等,以及新增的手势识别功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

再UIView中,可以重些以下四个方法来来控制用户的触摸动作:

 

 

  1. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
  2. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
  3. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
  4. - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
其中前三个方法很好理解,分别为触摸开始,移动,结束. 最后一个方法通常由系统掉用,比如在系统内存不够时,如果你正在触摸,那么这个方法可能会被得到掉用.但是一般情况下也不需要对此方法做处理.

通过NSSet count 可以获知触摸的指头的个数. Set中每一个元素就是UITouch对象.从UITouch对象中可以获取很多有用的信息.

[touch phase] 就可以获知触摸的方式,它是一个枚举类型来的.

 

typedef enum {

    UITouchPhaseBegan,             // whenever a finger touches the surface.

    UITouchPhaseMoved,             // whenever a finger moves on the surface.

    UITouchPhaseStationary,        // whenever a finger is touching the surface but hasn't moved since the previous event.

    UITouchPhaseEnded,             // whenever a finger leaves the surface.

    UITouchPhaseCancelled,         // whenever a touch doesn't end but we need to stop tracking (e.g. putting device to face)

} UITouchPhase;

 

 

touch tapCount 可以获取这个触摸的点击次数.(类似双击鼠标,如果双击就是2次)

touch timestamp 获取触摸的时间戳,这样可以计算一些点击的频率等应用.

touch locationInView: 获取坐标系,在某个view中

touch previousLocationInView: 获取在某个view中的前一次坐标系

touch gestureRecognizers 这个3.2新增加的(iPad),用于判断手势用

 

新增加的手势判断有以下这几种:

 

 

  • UILongPressGestureRecognizer
  • UIPanGestureRecognizer
  • UIPinchGestureRecognizer
  • UIRotationGestureRecognizer
  • UISwipeGestureRecognizer
  • UITapGestureRecognizer
如果想兼容早期的版本,并且不是在iPad中,那么手势暂时还是只能手动实现.不知道OS4是否已经在iPhone的SDK中增加了这个手势识别.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值