IOS开发:手势触摸事件

iOS中的事件

一 : 用户在使用APP过程中,会产生各种各样的触摸事件

二 :IOS中的事件可以分为三大类

   响应者对象

一 :在IOS中不是任何对象都能处理事件只有继承了UIResponder的对象才能接受并且处理响应事件,一般称之为"响应者对象"


二 :UIApplocation、UIViewControll、UIView、都继承自UIResponder,因此他们都是响应者对象,都能够接受并且处理响应事件,一般情况下的View不支持多点触碰,需要设置multipleTouch属性


 UIResponder

UIResponder内部提供了一下方法来处理事件。

// 普通UIView实现以下方法

效果

做法 A

    // 触摸对象
   UITouch* touch = [touches anyObject];
    // 取位置
   CGPoint point = [touch locationInView:self.superview];
    // 改坐标
    self.center = point;
做法B

    // 触摸对象
   UITouch* touch = [touches anyObject];
    // 取位置
   CGPoint current = [touch locationInView:self];
    // 上一个触摸点
    CGPoint previous = [touch previousLocationInView:self];
    // 改坐标
    CGPoint center = self.center;
    center.x+= current.x - previous.x;
    center.y+= current.y - previous.y;
    self.center = center;


每产生一个 事件 ,就会产生一个 UIEvent 对象

UIEvent :称为事件对象, 记录事件产生的 时刻 类型

常见属性
事件类型

@property(nonatomic,readonly) UIEventType     type;

@property(nonatomic,readonly) UIEventSubtype  subtype;

事件产生的时间

@property(nonatomic,readonly) NSTimeInterval  timestamp;

UIEvent 还提供了相应的方法可以获得在某个 view 上面的触摸对象( UITouch

touches和event参数

一次完整的触摸过程,会经历 3 个状态:

Ø 触摸开始: - ( 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

4 个触摸事件处理方法中,都有 NSSet *touches UIEvent *event 两个参数
Ø 一次完整的触摸过程中,只会产生一个事件对象 4 个触摸方法都是同一个 event 参数

Ø 如果两根手 指同时触摸 一个 view 那么 view 只会调用一次 touchesBegan:withEvent: 方法, touches 参数中装着 2 UITouch 对象

Ø 如果这两根手指 一前一后分开触摸同一个 view ,那么 view 会分别调用 2 touchesBegan:withEvent: 方法,并且每次调用时的 touches 参数中只包含一个 UITouch

Ø 根据 touches UITouch 的个数可以判断出是单点触摸还是多点触摸

UITouch的属性

触摸产生时所处 的窗

@property(nonatomic,readonly,retain) UIWindow    *window;

触摸产生时所处的视图

@property(nonatomic,readonly,retain) UIView      *view;

短时间内点按屏幕的次数,可以根据 tapCount 判断单击、双击或更 多的点击

@property(nonatomic,readonly) NSUInteger          tapCount;

记录了触摸事件产生或变化时的时间 ,单位是

@property(nonatomic,readonly) NSTimeInterval      timestamp;

当前触摸事件所处的状态

@property(nonatomic,readonly) UITouchPhase        phase;

    UITouch* touch = [touches anyObject];
    
    NSLog(@"%@ %d",[touch view],[touch tapCount]);






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值