美文网首页
iOS 手势与触摸

iOS 手势与触摸

作者: 苏宇lovecc | 来源:发表于2016-08-08 16:39 被阅读23次
    1. 响应触摸方法:
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        NSLog(@"touchesBegan");
    }
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
        NSLog(@"touchesMoved");
    }
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
        NSLog(@"touchesEnded");
    }
    //当系统事件(如内存不足、电话呼入)终止了触摸事件时响应该方法
    -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
        NSLog(@"touchesCancelled");
    }
    
    1. 添加了手势的控件常需要设置的属性:
      (UILabel,UIImageVIew默认不允许用户交互)
    //UILabel默认不允许用户交互,故如此设置
    _lbtext.userInteractionEnabled = YES;
    //设置控件为多点触控
     _lbtext.multipleTouchEnabled = YES;
    
    1. 手势处理器 UIGestureRecognizer
      UIGestureRecognizer是所有手势控制器的基类,它提供了如下子类:

      • UITapGestureRecognizer:轻拍手势
      • UIPinchGestureRecognizer : 捏合手势
      • UIPanGestureRecognizer :拖拽手势
      • UISwipeGestureRecognizer : 轻扫手势
      • UIRotationGestureRecognizer :旋转手势
      • UILongPressGestureRecognizer :长按手势
    2. UITapGestureRecognizer 轻拍 -常用于 “加在空白处,关闭键盘”

    UITapGestureRecognizer *tapGesture2 
    = [[UITapGestureRecognizer alloc] initWithTarget:self    action:@selector(closeKey)];
    [self.view addGestureRecognizer:tapGesture2];
    -(void)closeKey{
        [self.view endEditing:YES];
    }
    
    1. UISwipeGestureRecognizer 轻扫
      特有属性: dirction:设置轻扫的方向,支持上,下,左,右四个方向。
      (设置四个方向的轻扫)
    for (int i=0; i<4; i++) {
        UISwipeGestureRecognizer *swipeGesture 
        = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeHandle:)];
            [swipeGesture setDirection:1<<i];
            [self.view addGestureRecognizer:swipeGesture];
        }
    
    -(void)swipeHandle:(UISwipeGestureRecognizer *)recognizer{
        switch (recognizer.direction) {
            case UISwipeGestureRecognizerDirectionUp:
                NSLog(@"向上滑");
                break;
            case UISwipeGestureRecognizerDirectionDown:
                NSLog(@"向下滑");
                break;
            case UISwipeGestureRecognizerDirectionRight:
                NSLog(@"向右滑");
                break;
            case UISwipeGestureRecognizerDirectionLeft:
                NSLog(@"向左滑");
                break;
            default:
                break;
        }
    }
    
    1. 拖动时候肯定有个速度,返回值就是你拖动时X和Y轴上的速度,速度是矢量,有方向。
    CGPoint velocity = [(UIPanGestureRecognizer *)gestureRecognizer     velocityInView:self.view];
    

    参考资料:

    1. 知识链接1

    2. 知识链接2

    3. 响应链简析

    相关文章

      网友评论

          本文标题:iOS 手势与触摸

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