美文网首页
32.UITouch

32.UITouch

作者: 二斤寂寞 | 来源:发表于2020-11-04 11:18 被阅读0次
1.1 UITouch的创建

当用户用一根手指触摸屏幕时,会创建一个与手指相关联的UITouch对象,一根手指对应一个UITouch对象。

1.2 UITouch的作用

UITouch保存着跟手指相关的信息,比如触摸的位置、时间、阶段等。
当手指移动时,系统会更新同一个UITouch对象,使之能够一直保存该手指在的触摸位置。
当手指离开屏幕时,系统会销毁相应的UITouch对象。

1.3 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;

//Touch 状态枚举
typedef NS_ENUM(NSInteger, UITouchPhase) {
    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)
};
1.4 UITouch的常用方法
- (CGPoint)locationInView:(UIView *)view;
//返回值表示触摸在view上的位置
//这里返回的位置是针对view的坐标系的(以view的左上角为原点(0, 0))
//调用时传入的view参数为nil的话,返回的是触摸点在UIWindow的位置

- (CGPoint)previousLocationInView:(UIView *)view;
//记录了前一个触摸点的位置

相关文章

  • 32.UITouch

    1.1 UITouch的创建 当用户用一根手指触摸屏幕时,会创建一个与手指相关联的UITouch对象,一根手指对应...

网友评论

      本文标题:32.UITouch

      本文链接:https://www.haomeiwen.com/subject/qkafxktx.html