美文网首页
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 事件以及手势的处理

    iOS 事件以及手势的处理 首先引用深入浅出iOS事件机制,iOS触摸事件处理详解,详解iOS触摸事件与手势识别三...

  • 《iOS事件触摸与手势》

    iOS事件触摸与手势 一、事件分发处理【由外到内】在iOS中发生触摸后,事件会加到UIApplication事件队...

  • iOS 手势与触摸

    响应触摸方法: 添加了手势的控件常需要设置的属性:(UILabel,UIImageVIew默认不允许用户交互) 手...

  • 产品新人必备的iOS人机交互指南(转译)三

    2.10 手势 人们通过在触摸屏上执行手势来与iOS设备进行交互。这些手势与内容有着密切的个人联系,增强了屏幕对象...

  • 用户交互(9)-手势

    手势 用户通过在触摸屏上使用手势与iOS设备交互。这些手势引出了与内容的亲近个人联系,增强了对屏幕对象的直接操作感...

  • iOS之手势与触摸

      触摸是iOS的交互核心,他不简单局限于“按下按钮”或“点击键盘”,还包括手势识别在内的一系列手势; 触摸#  ...

  • iOS手势识别器

    1.手势识别器 1.手势识别器是iOS中比较抽象的一个类,用于识别一个手势,所谓手势:有规律的触摸。是对触摸事件做...

  • iOS 手势操作 geekband

    什么是触摸手势 触摸手势的原理 UIControl 离散与断续 手势识别状态 小案例 新建文件在storyboar...

  • 事件处理

    iOS事件分类以及相关方法 在iOS中事件分为三类: 触摸事件:通过触摸、手势进行触发(例如手指点击、缩放、旋转)...

  • iOS事件处理

    iOS事件分类以及相关方法 在iOS中事件分为三类: 触摸事件:通过触摸、手势进行触发(例如手指点击、缩放、旋转)...

网友评论

      本文标题:iOS 手势与触摸

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