zoukankan      html  css  js  c++  java
  • ios开发事件处理之:一:UIView的拖拽

    1.ios当中常⽤的事件?  触摸事件 ,加速计事件 ,远程控制事件

    2.什么是响应者对象? 继承了UIResponds的对象我们称它为响应者对象 UIApplication、UIViewController、UIView都继承⾃自UIResponder 因此它们都是响应者对象,都能够接收并处理事件

    3.为什么说继承了UIResponder就能够处理事件? 因为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;

    4.如何监听UIView的触摸事件? 想要监听UIViiew的触摸事件,⾸首先第⼀一步要⾃自定义UIView, 因为只有实现了UIResponder的事件⽅方法才能够监听事件.

    UIView的触摸事件主要有:

    ⼀一根或者多根⼿手指开始触摸view,系统会⾃自动调⽤用view的下⾯面⽅方法.

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

    ⼀一根或者多根⼿手指在view上移动时,系统会⾃自动调⽤用view的下⾯面⽅方法 (随着⼿手指的移动,会持续调⽤用该⽅方法,也就是说这个⽅方法会调⽤用很多次)

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

    ⼀一根或者多根⼿手指离开view,系统会⾃自动调⽤用view的下⾯面⽅方法
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

    ⼀一当突然发生系统事件的时候,例如突然来了系统电话,或是手机没电的时候,系统就会自动调用

    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

    参数说明:

        touches:

        touches中存放的都是UITouch对象,它是一个NSSet集合.NSSet和NSArray一样同样属于集合,但是NSSet是无序的,NSArray是有序的,有序的就可以通过index将对象从集合中取出

        UITouch对象它就是用来保存手指相关联的信息.包括位置,时间,阶段等信息.

        每一个手指对应着一个UITouch对象.

        这个UITouch是系统自动帮我们创建的,当手指移动时,系统会更新同一个UITouch对象,

        使它能够一直保存该手指在的触摸位置

        

        通过获取UITouch属性,我们可以获得触摸产生时所处的窗口,触摸的View,时间,点击的次数等,

        这些都可以在通过UITouch获取.

       

     UITouch方法:

    @property(nonatomic,readonly) NSTimeInterval      timestamp;//获取点击的时间

    @property(nonatomic,readonly) UITouchPhase        phase;//点击的状态

    @property(nonatomic,readonly) NSUInteger          tapCount;   //点击的次数

    @property(nullable,nonatomic,readonly,strong) UIWindow  *window;//点击的所在的window

    @property(nullable,nonatomic,readonly,strong) UIView *view;//点击所在的view

        event:

        还可以通过UITouch提供的方法获取当前手指所在的点,以及上一个手指所在的点.

        取当前手指所在的点

        - (CGPoint)locationInView:(UIView *)view;

        获取上一个触摸点的位置.

        - (CGPoint)previousLocationInView:(UIView *)view;

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

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

     一次完整的触摸过程,会经历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个触摸方法都是同一个event参数

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

    如果这两根手指一前一后分开触摸同一个view,那么view会分别调用2次touchesBegan:withEvent:方法,

    并且每次调用时的touches参数中只包含一个UITouch对象

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

    page2image376

    实现关键代码:
    当⼿手指在屏幕上移动时调⽤用持续调⽤用
    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]; X轴⽅方向偏移量

    CGFloat offsetX = curP.x - preP.x; Y轴⽅方向偏移量

    CGFloat offsetY = curP.y - preP.y; CGAffineTransformMakeTranslation:会清空上⼀一次的形变. self.transform = CGAffineTransformMakeTranslation(offsetX,

    0);

    self.transform = CGAffineTransformTranslate(self.transform, offsetX, offsetY);

    #import "RedView.h"
    
    @implementation RedView
    
    
    //当开始触摸屏幕的时候调用
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        NSLog(@"%s",__func__);
    }
    
    //触摸时开始移动时调用(移动时会持续调用)
    
    
    /*
     
      1:touches.allObjects:获得所有的触摸的手指UITouch对象:当只有一根手指触摸屏幕就会产生一个UITouch对象,一个UITouch对象通过[touches anyObject]获得
      2: CGPoint curP = [touch locationInView:self];
         CGPoint preP = [touch previousLocationInView:self];
      3:要累加形变:所以用CGAffineTransformTranslate(self.transform, offsetX, offsetY),初始状态self.transform为0
     */
    //NSSet:无序
    //NSArray:有序
    -(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        
        //做UIView拖拽
        UITouch *touch = [touches anyObject];
       
        
        //求偏移量 = 手指当前点的X - 手指上一个点的X
        CGPoint curP = [touch locationInView:self];
        CGPoint preP = [touch previousLocationInView:self];
        NSLog(@"curP====%@",NSStringFromCGPoint(curP));
        NSLog(@"preP====%@",NSStringFromCGPoint(preP));
        
        CGFloat offsetX = curP.x - preP.x;
        CGFloat offsetY = curP.y - preP.y;
        
        //平移
        self.transform = CGAffineTransformTranslate(self.transform, offsetX, offsetY);
        
    }
    
    //当手指离开屏幕时调用
    -(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
         NSLog(@"%s",__func__);
    }
    
    
    //当发生系统事件时就会调用该方法(电话打入,自动关机)
    -(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
         NSLog(@"%s",__func__);
    }
    
    
    
    @end
  • 相关阅读:
    力扣 136. 只出现一次的数字
    剑指 Offer 24. 反转链表
    javaWeb8——jdbc总结,JDBC调用存储过程和存储函数:CallableStatement
    剑指 Offer 53
    剑指 Offer 53
    使用 .NET Core 3.x 构建 RESTFUL Api
    SqlServer 多表连接、聚合函数、模糊查询、分组查询应用总结(回归基础)
    SqlServer 查询的几种方式以及数字函数、时间函数的应用总结(回归基础)
    关于SqlServer表结构 2(回归基础)
    关于SqlServer那些事1(回归基础)
  • 原文地址:https://www.cnblogs.com/cqb-learner/p/5809887.html
Copyright © 2011-2022 走看看