iOS---touch使用注意事项

本文详细介绍了在iOS开发中如何处理触控事件,包括不同类型的UITouch方法在UIView与UIViewController中的使用区别,以及如何通过代码获取触控信息。此外,还提供了在模拟器中调试多点触摸的方法。

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

1。当在UIView中使用UITouch时,重写方法

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
   - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
   - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

中使用UITouch *touch=[touches anyObject];获取事件是可以的,

但当类继承自UIViewController时,

则要使用UITouch *touch=[[event allTouches] anyObject];来获取

至于要从UITouch中获取什么信息,你可以使用

NSLog(@"UITouch Info:%@",touch);在Console中查看。

 

2.在模拟器中要调试多点触摸可以:

               鼠标放在模拟器上,按住Option就出来2个点了,然后可以鼠标来调试了,

前提要设置 UIView  的 self.view.multipleTouchEnabled = YES;(支持多点触摸)

以下是多点触摸获取信息方法:

  NSSet *allTouches = [event allTouches];
 //[allTouches count]获得当前touch的是否是多触摸,如果 [allTouches count] == 1就不是多触摸。
    UITouch *touch0 = [[allTouches allObjects] objectAtIndex:0];
    UITouch *touch1 = [[allTouches allObjects] objectAtIndex:1];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值