zoukankan      html  css  js  c++  java
  • 手势识别

    1、点击手势

    - (void)testTap

    {

        // 1.创建手势识别器对象

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];

        // 连续敲击2次,手势才能识别成功

        tap.numberOfTapsRequired = 2;

        tap.numberOfTouchesRequired = 2;

        

        // 2.添加手势识别器对象到对应的view

        [self.iconView addGestureRecognizer:tap];

        

        // 3.添加监听方法(识别到了对应的收拾,就会调用监听方法)

        [tap addTarget:self action:@selector(tapView)];

    }

    /**

     *  当点击view的时候,会先调用这个方法

     */

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch

    {

        CGPoint pos = [touch locationInView:touch.view];

        if (pos.x <= self.iconView.frame.size.width * 0.5) {

            return YES;

        }

        return NO;

    }

    /**

     *  当点击view的时候,会先调上面的方法,然后再调下面的方法

     */

    - (void)tapView

    {

        NSLog(@"-----tapView");

    }

    2、轻扫手势

    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeView)];

    swipe.direction = UISwipeGestureRecognizerDirectionUp;

    [self.redView addGestureRecognizer:swipe];

    - (void)swipeView

    {

        NSLog(@"swipeView");

    }

    3、 长按手势

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] init];

    [longPress addTarget:self action:@selector(longPressView)];

    // 至少长按2秒

    longPress.minimumPressDuration = 2;

     // 在触发手势之前,50px范围内长按有效

     longPress.allowableMovement = 50;    

    [self.redView addGestureRecognizer:longPress];

    - (void)longPressView

    {

        NSLog(@"长按了红色的view");

    }

    4、 缩放手势(捏合手势)

    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchView:)];

    pinch.delegate = self;

    [self.iconView addGestureRecognizer:pinch];

    - (void)pinchView:(UIPinchGestureRecognizer *)pinch

    {

    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);

        pinch.scale = 1; // 还原缩放比例,

    }

    5、旋转手势

    UIRotationGestureRecognizer *recognizer = [[UIRotationGestureRecognizeralloc] initWithTarget:self action:@selector(rotateView:)];

        recognizer.delegate = self;

        [self.iconView addGestureRecognizer:recognizer];

    - (void)rotateView:(UIRotationGestureRecognizer *)recognizer

    {

        recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);

        recognizer.rotation = 0; // 这个很重要!!!!!

    }

    6、拖拽平移手势

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];

    [self.purpleView addGestureRecognizer:pan];

    - (void)panView:(UIPanGestureRecognizer *)pan

    {

        switch (pan.state) {

            case UIGestureRecognizerStateBegan: // 开始触发手势

                break;

            case UIGestureRecognizerStateEnded: // 手势结束

                break;

            default:

                break;

        }

        // 1.在view上面挪动的距离

        CGPoint translation = [pan translationInView:pan.view];

        CGPoint center = pan.view.center;

        center.x += translation.x;

        center.y += translation.y;

        pan.view.center = center;

        // 2.清空移动的距离

        [pan setTranslation:CGPointZero inView:pan.view];

    }

    7、同时允许多个手势

    1->实现以下方法,让控制器可以同时识别多个手势

    /**

     *  是否允许多个手势识别器同时有效

     *  Simultaneously : 同时地

     */

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer

    {

        return YES;

    }

    2->遵守UIGestureRecognizerDelegate协议

    3->哪几手势需要同时识别,则这每个手势都要设置代理

     
  • 相关阅读:
    vue table 中 列 加上 下划线和click 方法
    vue 比较好的学习文章
    Hive 以及mysql 中如何做except 数据操作
    oracle 日期维表 原始版本 带注解
    RMI 实现的rpc 远程过程调用 Java
    剑指offer20:定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。
    剑指offer19:按照从外向里以顺时针的顺序依次打印出每一个数字,4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
    模拟通讯录
    剑指offer17:输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
    剑指offer16:输入两个单调递增的链表,合成后的链表满足单调不减规则。
  • 原文地址:https://www.cnblogs.com/bluceZ/p/3938417.html
Copyright © 2011-2022 走看看