IOS UITouch 触摸事件,UIResponder 的作用

IOS 中事件响应皆是通过UIResponder。我们可以执行UIResponder协议响应事件。这里我们用UIViewController做示例,UIViewController默认实现UIResponder协议。

// UIViewController.m
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"屏幕被手指按下了");
    // 获取手指列表
    NSArray<UITouch*>* toucheList = [touches allObjects];
    for (int i = 0; i < toucheList.count; i ++) {
        UITouch* touch = toucheList[i];
        NSLog(@"获取手指15s内同一位置点击次数 -> 手指 %d 15s内点击了 %zd 次",  i, touch.tapCount);
    }
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"手指移动啦");
    // 获取手指列表
    UITouch* touch = [touches allObjects][0];
    // 获取手指相对于指定视图的x,y轴
    CGPoint position = [touch locationInView:self.view];
    NSLog(@"手指坐标为x = %f,y = %f", position.x, position.y);
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"手指离开了");
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    NSLog(@"触摸过程被其他行为异常中断");
}
UIResponder 协议函数
  • - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event 当手指触摸屏幕触发
  • - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event 当手指触摸并产生移动触摸
  • - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event 当手指离开屏幕触发
  • - (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event 当手指触摸过程被其他行为中断等异常情况触发
  • - (void)pressesBegan:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event API_AVAILABLE(ios(9.0)) 用户用力按下屏幕开始触发
  • - (void)pressesChanged:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event API_AVAILABLE(ios(9.0)) 用户用力按下屏幕发生压力变化触发
  • - (void)pressesEnded:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event API_AVAILABLE(ios(9.0)) 用户按压结束触发
  • - (void)pressesCancelled:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event 用户按压行为被打断触发,IOS9以上支持
  • - (void)motionBegan:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event 陀螺仪或加速设备开始发生改变触发
  • - (void)motionEnded:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event 陀螺仪或加速设备发生改变过程触发
  • - (void)motionCancelled:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event 陀螺仪或加速设备改变过程被其他行为中断异常触发
UITouch 属性
名称类型说明默认值
timestampNSTimeInterval手指当前触摸的时间
phaseUITouchPhase手指当前处于的行为状态
tapCountNSUInteger手指15秒内同一位置触摸的次数0
typeUITouchType手指是间接还是直接触摸屏幕
typeUITouchType手指是间接还是直接触摸屏幕
majorRadiusCGFloat手指触摸屏幕的半径
maximumPossibleForceCGFloat设备最大压力力度
maximumPossibleForceCGFloat设备最大压力力度
UITouch API
  • - (CGPoint)locationInView:(nullable UIView *)view 手指当前相对于指定视图的位置
  • - (CGPoint)previousLocationInView:(nullable UIView *)view 手指上一次相对于指定视图的位置
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杨周龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值