No5 触摸事件

一 Modal

  • 除了push之外,还有另外一种控制器的切换方式,那就是Modal
  • 任何控制器都能通过Modal的形式展示出来
  • Modal的默认效果:新控制器从屏幕的最底部往上钻,直到盖住之前的控制器为止

  • 以Modal的形式展示控制器

- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^)(void))completion
  • 关闭当初Modal出来的控制器
- (void)dismissViewControllerAnimated: (BOOL)flag completion: (void (^)(void))completion;

modal:会把新控制器的view添加窗口上,但是不会修改窗口的根控制器
modal:会把新控制器强引用,谁modal,谁就强引用,为什么要强引用,如果不强引用,新创建的控制器就会被销毁,就不能处理modal出来界面的业务逻辑.

  • modal原理解析
 // 从下往上钻的动画
 // 首先让oneVc的view显示在窗口的底部
    oneVc.view.transform = CGAffineTransformMakeTranslation(0, keyWindow.bounds.size.height);   
 // 动画,往上移动,还原形变
    [UIView animateWithDuration:0.5 animations:^{
//   还原形变
//  CGAffineTransformIdentity清空所有的形变,所有的形变参数都是0
       oneVc.view.transform = CGAffineTransformIdentity;
      }];
// transform:可以用来做控件的形变,平移,缩放,旋转

二 iOS中的事件

  • 事件分3大类型
    • 触摸事件 加速计事件 远程控制事件
  • 响应者对象
    • 在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件。我们称之为“响应者对象”
    • UIApplication、UIViewController、UIView都继承自UIResponder,因此它们都是响应者对象,都能够接收并处理事件
  • 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;
  • UIView的触摸事件处理
    • UIView是UIResponder的子类,可以覆盖下列4个方法处理不同的触摸事件
//  一根或者多根手指开始触摸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

//  触摸结束前,某个系统事件(例如电话呼入)会打断触摸过程,系统会自动调用view的下面方法
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
// 提示:touches中存放的都是UITouch对象
  • UITouch
    • 当用户用一根手指触摸屏幕时,会创建一个与手指相关联的UITouch对象
    • 一根手指对应一个UITouch对象
    • UITouch的作用
      • 保存着跟手指相关的信息,比如触摸的位置、时间、阶段
    • 当手指移动时,系统会更新同一个UITouch对象,使之能够一直保存该手指在的触摸位置
    • 当手指离开屏幕时,系统会销毁相应的UITouch对象

提示:iPhone开发中,要避免使用双击事件!

  • 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;
  • UITouch的方法
- (CGPoint)locationInView:(UIView *)view;
// 返回值表示触摸在view上的位置
// 这里返回的位置是针对view的座标系的(以view的左上角为原点(0, 0))
// 调用时传入的view参数为nil的话,返回的是触摸点在UIWindow的位置

- (CGPoint)previousLocationInView:(UIView *)view;
// 该方法记录了前一个触摸点的位置
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 想让控件随着手指移动而移动,监听手指移动
    // 获取UITouch对象
    UITouch *touch = [touches anyObject];

    // 获取当前点的位置
    CGPoint curP = [touch locationInView:self];

    // 获取上一个点的位置
    CGPoint preP = [touch previousLocationInView:self];

    // 获取它们x轴的偏移量,每次都是相对上一次
    CGFloat offsetX = curP.x - preP.x;

    // 获取y轴的偏移量
    CGFloat offsetY = curP.y - preP.y;

    // 修改控件的形变或者frame,center,就可以控制控件的位置
    // 形变也是相对上一次形变(平移)
    // CGAffineTransformMakeTranslation:会把之前形变给清空,重新开始设置形变参数
    // make:相对于最原始的位置形变
    // CGAffineTransform t:相对这个t的形变的基础上再去形变
    // 如果相对哪个形变再次形变,就传入它的形变
    self.transform = CGAffineTransformTranslate(self.transform, offsetX, offsetY);
}

三 事件的产生和传递

  • UIEvent

    • 每产生一个事件,就会产生一个UIEvent对象
    • UIEvent:称为事件对象,记录事件产生的时刻和类型
    • 常见属性
      //  事件类型
      @property(nonatomic,readonly) UIEventType     type;
      @property(nonatomic,readonly) UIEventSubtype  subtype;
      //  事件产生的时间
      @property(nonatomic,readonly) NSTimeInterval  timestamp;
      
  • 一次完整的触摸过程,会经历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
  • 事件的产生和传递
    • 发生触摸事件后,系统会将该事件加入到一个由UIApplication管理的事件队列中
    • UIApplication会从事件队列中取出最前面的事件,并将事件分发下去以便处理,通常,先发送事件给应用程序的主窗口(keyWindow)
    • 主窗口会在视图层次结构中找到一个最合适的视图来处理触摸事件,这也是整个事件处理过程的第一步
    • 找到合适的视图控件后,就会调用视图控件的touches方法来作具体的事件处理
      • touchesBegan…
      • touchesMoved…
      • touchedEnded…
  • 实例:
    这里写图片描述

  • UIView不接收触摸事件的三种情况

    • 不接收用户交互:userInteractionEnabled = NO
    • 隐藏 hidden = YES
    • 透明 alpha = 0.0 ~ 0.01

提示:UIImageView的userInteractionEnabled默认就是NO,因此UIImageView以及它的子控件默认是不能接收触摸事件的

四 hitTest 方法

  • hitTest
    • 产生触摸事件 -> UIApplication事件队列 -> [UIWindow hitTest] 去寻找最合适的view
// 什么时候调用:只要事件一传递给一个控件就会调用
// 作用:寻找最合适的view给你
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
   UIView *fitView = [super hitTest:point withEvent:event];
   return fitView;   
}

使用return nil,可以拦截事件传递过程,想让谁处理事件谁处理事件

  • 方法底层实现代码如下:
// UIApplication -> [UIWindow hitTest:withEvent:]寻找最合适的view告诉系统
// point:当前手指触摸的点
// point:是方法调用者座标系上的点
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
//   UIView *fitView = [super hitTest:point withEvent:event];
    // 1.判断下窗口能否接收事件
    if (self.userInteractionEnabled == NO || self.hidden == YES || self.alpha <= 0.01) return nil;

    // 2.判断下点在不在窗口上
    // 不在窗口上
    if ([self pointInside:point withEvent:event] == NO) return nil;

    // 3.从后往前遍历子控件数组
    int count = (int)self.subviews.count;
    // 0 1
    for (int i = count - 1; i >= 0; i--) {
        // 获取子控件
        UIView *childView = self.subviews[i];

        // 座标系的转换,把窗口上的点转换为子控件上的点
        // 把自己控件上的点转换成哪个控件上点
        CGPoint childP = [self convertPoint:point toView:childView];

         UIView *fitView = [childView hitTest:childP withEvent:event];

        if (fitView) {// 如果能找到最合适的view
            return fitView;
        }
    } 
    // 4.没有比自己更合适的view
    return self;
}
  • 判断传入过来的点在不在方法调用者的座标系上
// point:是方法调用者座标系上的点
 - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
 {
    return NO;
 }

五 响应者链条

  • 触摸事件处理的详细过程
    • 用户点击屏幕后产生一个触摸事件,经过一系列传递过程后,会找到最合适的视图控件来处理这个事件
    • 找到最合适的视图控件后,就会调用控件的touches方法来作事件处理
    • touches方法的默认做法是将事件顺着响应者链条向上传递,将事件交给上一个响应者进行处理
  • 响应者链条:是由多个响应者对象连接起来的链条
    • 作用:能很清楚的看见每个响应者之间的联系,并且可以让一个事件多个对象处理。
  • 事件传递的完整过程
    • 1> 先将事件对象由上往下传递(由父控件传递给子控件),找到最合适的控件来处理这个事件。
    • 2> 调用最合适控件的touches….方法
    • 3> 如果调用了[super touches….];就会将事件顺着响应者链条往上传递,传递给上一个响应者
    • 4> 接着就会调用上一个响应者的touches….方法
  • 如何判断上一个响应者

    • 1> 如果当前这个view是控制器的view,那么控制器就是上一个响应者
    • 2> 如果当前这个view不是控制器的view,那么父控件就是上一个响应者
  • 响应者链的事件传递过程

    • 1.如果view的控制器存在,就传递给控制器;如果控制器不存在,则将其传递给它的父视图
    • 2.在视图层次结构的最顶级视图,如果也不能处理收到的事件或消息,则其将事件或消息传递给window对象进行处理
    • 3.如果window对象也不处理,则其将事件或消息传递给UIApplication对象
    • 4.如果UIApplication也不能处理该事件或消息,则将其丢弃

六 手势识别

  • 通过touches方法监听view触摸事件,有很明显的几个缺点
    • 必须得自定义view
    • 由于是在view内部的touches方法中监听触摸事件,因此默认情况下,无法让其他外界对象监听view的触摸事件
    • 不容易区分用户的具体手势行为
  • 手势识别器—-UIGestureRecognizer
    • UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势
 UITapGestureRecognizer(敲击)
 UIPinchGestureRecognizer(捏合,用于缩放)
 UIPanGestureRecognizer(拖拽)
 UISwipeGestureRecognizer(轻扫)
 UIRotationGestureRecognizer(旋转)
 UILongPressGestureRecognizer(长按)
  • 轻扫手势
- (void)setUpSwipe
{
    // 轻扫
    // 一个手势只能对应一个方向
    // 默认轻扫的方向往左
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];

    // 设置轻扫的方向
    swipe.direction = UISwipeGestureRecognizerDirectionLeft;

    [_imageView addGestureRecognizer:swipe];

    // 默认轻扫的方向往右
    UISwipeGestureRecognizer *swipeR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];

    // 设置轻扫的方向
    swipeR.direction = UISwipeGestureRecognizerDirectionRight;

    [_imageView addGestureRecognizer:swipeR];
}


- (void)swipe:(UISwipeGestureRecognizer *)swipe
{
    if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
        // 往右边轻扫   
    }else{ 
        // 往左边轻扫
        NSLog(@"%s左边",__func__);
    }
}
  • 添加长按手势
- (void)setUpLongPress
{
    // 长按手势
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];

    [_imageView addGestureRecognizer:longPress];
}

// 什么时候调用:长按的时候调用,而且只要手指不离开,拖动的时候会一直调用,手指擡起的时候也会调用
- (void)longPress:(UILongPressGestureRecognizer *)longPress
{
    // 注意:在以后开发中,长按手势一般需要做判断
    if (longPress.state == UIGestureRecognizerStateEnded) {   
        NSLog(@"%s",__func__);
    }
}
  • 添加点按手势,需要添加代理tap.delegate = self;
  • 添加旋转手势
- (void)setUpRotation
{
    // 旋转
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];

    rotation.delegate = self;

    [_imageView addGestureRecognizer:rotation];
}
- (void)rotation:(UIRotationGestureRecognizer *)rotation
{
    // 获取的角度是相对于最原始的角度
    NSLog(@"%f",rotation.rotation);
    // 旋转图片
    _imageView.transform = CGAffineTransformRotate(_imageView.transform, rotation.rotation);
    // 复位,只要想相对于上一次旋转就复位
    rotation.rotation = 0;   
}
  • 缩放手势
- (void)setUpPinch
{
    // 捏合
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];

    pinch.delegate = self;

    [_imageView addGestureRecognizer:pinch];
}

- (void)pinch:(UIPinchGestureRecognizer *)pinch
{ 
    _imageView.transform = CGAffineTransformScale(_imageView.transform, pinch.scale, pinch.scale);
    // 复位
    pinch.scale = 1;
}
  • 如果同时添加缩放和旋转手势,代理方法
// Simultaneously:同时
// 是否同时支持多个手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}
  • 拖拽手势
- (void)pan:(UIPanGestureRecognizer *)pan
{

    // 获取手指平移的偏移量
    CGPoint transP = [pan translationInView:_imageView];

    _imageView.transform = CGAffineTransformTranslate(_imageView.transform, transP.x, transP.y);

    // 复位
    [pan setTranslation:CGPointZero inView:_imageView];
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章