美文网首页
iOS学习笔记(5):UITouch&UIEvent

iOS学习笔记(5):UITouch&UIEvent

作者: SevEnOye | 来源:发表于2016-10-06 23:44 被阅读0次
    • 当用户触摸屏幕时, 会创建一个与手指相关关联的UITouch对象
    • 一个手指对应一个UITouch对象

    UITouch作用

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

    UITouch的属性

    // 触摸产生时所处的窗口
    @property(nonatmoic, readonly, retain) UIWindow *window;
    // 触摸产生时所处的视图
    @property(nonatmoic, readonly, retain) UIView *view;
    // 短时间内点按屏幕的次数, 可以根据tapCount判断单击、双击或更多的点击
    @property(nonatmoic, readonly) NSUInteger tapCount;
    // 记录了触摸事件产生或变化时的时间,单位是秒
    @property(nonatomic, readonly) NSTimeInterval timestamp;
    // 当前触摸事件所处的状态
    @property(nonatomic, readonly) UITouchPhase phase;
    

    UITouch的方法

    // 返回值表示触摸在view上的位置
    // 这里返回的位置是针对view的坐标系的(以view的左上角为原点(0,0))
    // 调用时传入的view参数为nil的话,返回的是触摸点在UIWindow的位置
    - (CGPoint)locationInView:(UIView *)view;
    // 该方法记录了前一个触摸点的位置
    - (CGPoint)previousLocationInView:(UIView *)view;
    

    view的拖拽

     // 让当前控件随着手指移动而移动
     // 获取UITouch
     UITouch *touch = [touches anyObject];
     // 获取当前点
     CGPoint curP = [touch locationInView:self];
     // 获取上一个点
     CGPoint preP = [touch previousLocationInView:self];
     // 计算手指x轴偏移量
     CGFloat offsetX = curP.x - preP.x;
     // 计算手指x轴偏移量
     CGFloat offsetY = curP.y - preP.y;
     // 修改控件的形变
     self.transform = CGAffineTransformTranslate(self.transform, offsetX, offsetY);
    

    UIEvent

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

    常见属性

    // 事件类型
    @property(nonatomic, readonly) UIEventType type;
    @property(nonatomic, readonly) UIEventSubtype subtype;
    // 事件产生的时间
    @property(nonatomic, readonly) NSTimeInterval timestamp;
    

    相关文章

      网友评论

          本文标题:iOS学习笔记(5):UITouch&UIEvent

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