美文网首页基础
iOS 个人总结-UIGestureRecognizer\响应者

iOS 个人总结-UIGestureRecognizer\响应者

作者: Zonpai | 来源:发表于2018-08-05 22:12 被阅读0次

    作者也是前不久开始接触iOS,若是路过的业界大佬,就此跳过吧。作者只想通过此文记录一下最近在学习的内容,加深印象。


    一、UIResponder

    以UIResponder作为超类的任何对象都可以成为响应者。UIView以及UIViewController都是UIResponder的子类。
    即UIView和UIViewcontroller及它们的子类都可以作为响应者对象监听用户手势。

    注意
    UIImageView 也可以作为响应者,前提将其属性userInteractionEnabled设置为YES;
    imageView.userInteractionEnabled = YES;
    由此注意点,作者想到了当一个view不能响应事件的三种时机:
    userInteractionEnable = NO;hidden = YES;alpha = 0~0.01;


    iOS中事件的类型:触摸事件、加速计事件、远程控制事件

    UIResponder中部分方法

    //触摸API
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;
    //按压API
    - (void)pressesBegan:(NSSet<UIPress *> *)presses withEvent:(nullable UIPressesEvent *)event
    //加速API �(微信摇一摇)
    - (void)motionBegan:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event
    

    然后通过touches中一个方法实现了一个小demo
    代码:

    @implementation RedView
    - (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        //获取touches中的任意UITouch对象
        //一个UITouch对象代表一个手指,其生命周期从手指接触屏幕开始到手指离开屏幕结束
        UITouch *touch = [touches anyObject];
        CGPoint curPoint = [touch locationInView:self];
        CGPoint prePoint = [touch previousLocationInView:self];
        //计算相对位移
        CGFloat sx = curPoint.x - prePoint.x;
        CGFloat sy = curPoint.y - prePoint.y;
        self.transform = CGAffineTransformTranslate(self.transform, sx, sy);
    }
    @end
    

    效果图:(实现redView的拖拽)


    QQ20180805-201621-HD.gif

    二、响应者链

    响应者链是一个能够响应用户事件的可变对象集合。

    执行机制:

    如果响应者不处理某个特殊事件,那么它通常会将事件传递到响应者链的下一级。如果该链中下一个对象响应此特殊事件,则它会处理此事件,并停止事件的传递过程。

    当发生一个触摸事件后,系统会将事件加入到一个由UIApplication管理的事件队列(FIFO原则)中,UIApplication会从事件中取出最前面的事件,并将事件分发下去以便处理,主穿口会在视图层次结构中找到一个最合适的视图来处理触摸事件。

    注意
    1.寻找最合适的响应者是从最底层往上找的,如 window —> view —> child View
    2.响应者链是从最上层往底层传递的,如 child view —> view —> view controller —> window —> Application

    QQ20180805-205056.png

    3.触摸事件的传递是从父视图传递给子视图。
    4.如果一个父控件不能响应事件,则其子视图也不能响应事件。


    三、UIGestureRecognizer

    相对于上述的方法,UIGestureRecognizer实现手势更加方便,不需要另外创建一个子类。

    手势类型

        UITapGestureRecognizer *tapGs;//点按手势
        UILongPressGestureRecognizer *longPressGs;//长按手势
        UIPanGestureRecognizer *panGs;//拖动手势
        UISwipeGestureRecognizer *swipeGs;//轻扫手势
        UIRotationGestureRecognizer *rotationGs;//旋转手势
        UIPinchGestureRecognizer *pinchGs;//捏合手势
    

    每种类型的手势都有自己对应的独有属性及方法,例如
    UIPanGestureRecognizer中有如下(作者常用到)

    - (void)setTranslation:(CGPoint)translation inView:(nullable UIView *)view;
    

    又如UIPinchGestureRecognizer中
    @property (nonatomic) CGFloat scale; //缩放比例
    作者只是举了两个例子,在这里就不一一赘述

    这里我就使用rotation手势来实现一个旋转(rotation):
    代码:

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.pinkView.userInteractionEnabled  = YES;
        UIRotationGestureRecognizer *rotationGs = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
        [self.pinkView addGestureRecognizer:rotationGs];
    }
    - (void)rotationAction:(UIRotationGestureRecognizer *)rotationGs {
        self.pinkView.transform = CGAffineTransformRotate(self.pinkView.transform, rotationGs.rotation);
        //注意要重制rotation为0,否则rotation会累计
        [rotationGs setRotation:0];
    }
    

    效果图:(实现ImageView旋转)


    旋转手势.gif

    四、UIGestureRecognizerDelegate

    作者也只是列出了协议中的部分方法

    //是否监听手势
    - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer;
    
    // 是否允许同时支持多个手势,默认是不支持多个手势
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
    
    // 手指触摸屏幕后回调的方法,手势识别
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
    

    注意
    iOS中默认是不支持多个手势,若要实现多个手势,就要实现UIRegestureRecognizerDelegate中的一个方法

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
            return YES;
    }
    

    这里,我就用协议使用了两个手势
    (代码差不多,就不上了)
    效果:


    旋转&平移.gif

    这是我的第一篇文,知识偏基础、简单,主要是想体验下写文的方式,了解下markdown语法的使用,制作gif,制作概念图,同时也记录一下自己学习的内容。
    自己所在工作室的师兄都很厉害,向他们学习hiahiahiahia。

    文中若有知识漏洞,还请路过的读者指正,谢谢~

    相关文章

      网友评论

        本文标题:iOS 个人总结-UIGestureRecognizer\响应者

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