zoukankan      html  css  js  c++  java
  • 动画之事件处理

    触摸事件简介

    响应者对象

    • 只有继承了UIResponder的对象才能接受并处理对象事件,我们称为响应者对象
    • UIApplication,UIViewController,UIView等是响应者对象

    UIResponder内部提供的处理事件的方法

    比如触摸事件会调⽤用以下⽅方法:
    - (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; 
    加速计事件会调⽤用:
    - (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event;
    - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event;
    - (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event; 
    远程控制事件会调⽤用:
    - (void)remoteControlReceivedWithEvent:(UIEvent *)event;
    

    UIView拖拽

    • 思路

      • 1.自定义UIView,实现监听⽅法.
      • 2.确定在TouchMove⽅法当中进⾏操作,因为⽤户⼿指在视图上移动的时候才需要移动视图。
      • 3.获取当前手指的位置和上⼀个⼿指的位置.
      • 4.当前视图的位置 = 上一次视图的位置 - ⼿手指的偏移量
    • 代码

      当⼿指在屏幕上移动时调⽤,持续调用
      NSSet:⾥面的元素都是⽆无序的.
      NSArray:⾥面的有顺序的.
      -(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
      // 1.获取⼿指的对象
      

    UITouch *touch = [touches anyObject];
    // 2.获取当前⼿指所在的点.
    CGPoint curP = [touch locationInView: self];
    // 3.获取⼿指的上⼀个点.
    CGPoint preP = [touch previousLocationInView: self];
    CGFloat offsetX = curP.x - preP.x; //X轴⽅方向偏移量
    CGFloat offsetY = curP.y - preP.y; //Y轴⽅方向偏移量
    // CGAffineTransformMakeTranslation:会清空上一次的形变.
    // self.transform = CGAffineTransformMakeTranslation(offsetX,0);
    self.transform = CGAffineTransformTranslate(self.transform, offsetX, offsetY);
    }
    ```

    事件的产生和传递

    • 产生和传递:
      • 当发⽣一个触摸事件后,系统会将该事件⼊入到一个由UIApplication管理的事件队列中.
      • UIApplication会从事件队列中取出最前面的事件,交给主窗⼝keyWindow.
      • 主窗口会在视图层次结构中找到一个最合适的视图来处理触摸事件
      • 触摸事件的传递是从父控件传递到子控件的.
      • 如果一个父控件不能接收事件,那么它⾥面的子控件也不能够接收事件.
    • 什么时候控件不能接受事件
      • 1.不接收⽤户交互时不能够处理事件
        userInteractionEnabled = NO
      • 2.当一个控件隐藏的时候不能够接收事件 Hidden = YES的时候
      • 3.当一个控件为透明白时候也不能够接收事件 alpha < 0.01的时候
      • 注意:UIImageViewuserInteractionEnabled默认就是NO, 因此UIImageView以及它的子控件默认是不能接收触摸事件的

    最合适的View---(hitText方法)

    • 1.先判断⾃己是否能够接收触摸事件,如果能再继续往下判断,
    • 2.再判断触摸的当前点在不在⾃己的身上.
    • 3.如果在⾃己身上,它会从后往前遍历子控件,遍历出每一个⼦控件后,重复前面的两个步骤.
    • 4.如果没有符合条件的子控件,那么它⾃己就是最适合的View.
  • 相关阅读:
    不可小视视图对效率的影响力
    Maximum Margin Planning
    PhysicsBased Boiling Simulation

    Learning Behavior Styles with Inverse Reinforcement Learning
    Simulating Biped Behaviors from Human Motion Data
    Nearoptimal Character Animation with Continuous Control
    Apprenticeship Learning via Inverse Reinforcement Learning
    回报函数学习的学徒学习综述
    Enabling Realtime Physics Simulation in Future Interactive Entertainment
  • 原文地址:https://www.cnblogs.com/LongLJ/p/6110255.html
Copyright © 2011-2022 走看看