美文网首页
IOS-手势事件简述

IOS-手势事件简述

作者: 小行为 | 来源:发表于2016-02-03 22:29 被阅读130次

在iOS中事件分为三类:

触摸事件:通过触摸。手势进行触发事件(七大手势)
运动时间:通过加速器进行触发的事件(摇一摇)
远程控制事件:通过其他远程设备进行触发(如:遥控)

在iOS中只有继承自UIResponder类的对象才能处理事件(如UIView.UIViewController.UIApplication这些继承自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 NS_AVAILABLE_IOS(3_0); 运动开始时执行;
  • (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event NS_AVAILABLE_IOS(3_0); 运动结束后执行;
  • (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event NS_AVAILABLE_IOS(3_0); 运动被意外取消时执行;
    远程控制事件
  • (void)remoteControlReceivedWithEvent:(UIEvent *)event NS_AVAILABLE_IOS(4_0); 接收到远程控制消息时执行;
#import "TouchView.h"
#import "UIColor+RandomColor.h"
// 延展
@interface TouchView ()

@property(nonatomic,strong)UIView *textView;
@property(nonatomic,assign)CGFloat time1;
@property(nonatomic,assign)CGFloat time2;

@end

@implementation TouchView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.textView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
        self.textView.backgroundColor = [UIColor greenColor];
        [self addSubview:self.textView];
    }
    return self;
}

// 触摸开始
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"%s -- %d",__FUNCTION__,__LINE__);
    
    UITouch *touch  = [touches anyObject];
    // 获取时间戳
    _time1 = touch.timestamp;
    
}

// 触摸移动
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"%s -- %d",__FUNCTION__,__LINE__);
    self.textView.backgroundColor = [UIColor randomColor];
    // 取任意手指
    UITouch *touch = [touches anyObject];
    // 获取先前的点
    CGPoint point1 = [touch previousLocationInView:_textView];
    // 获取现在的点
    CGPoint point2 = [touch locationInView:_textView];
    // 偏移量
    CGFloat dx = point2.x - point1.x;
    CGFloat dy = point2.y - point1.y;
    // 重新计算center
    CGPoint newCenter = CGPointMake(_textView.center.x+dx, _textView.center.y+dy);
    // 改变textView的center
    _textView.center = newCenter;
    

}

// 触摸结束
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    _time2 = touch.timestamp;
    //时间差
    NSLog(@"总计时间:%lf",_time2 - _time1);
    NSLog(@"%s -- %d",__FUNCTION__,__LINE__);

}

// 触摸取消
-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"%s -- %d",__FUNCTION__,__LINE__);

}

这样点击事件我们知道了

触摸的七大手势:tap(轻拍),rotation(旋转),swipe(轻扫),pinch(捏合),pan(平移),longPress(长按),screenEdgePan(屏幕边界平移);

// 1创建手势,并初始化,使用initWithTarget:antion:的方法创建
   UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapView:)];
   
   // 2设置属性
   // 轻拍次数
   tap.numberOfTapsRequired = 2;
   // 手指个数
   tap.numberOfTouchesRequired = 2;
   // 3添加到视图上
   [_redView addGestureRecognizer:tap];

UIScreenEdgePanGestureRecognizer需要注意两点

UIScreenEdgePanGestureRecognizer *screEdgePan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(screeEdgePan:)];
    // 属性设置
    // 注意: 使用屏幕边缘平移,注意两点
    // 1. 视图的位置
    // 2. 设置edges的属性
    screEdgePan.edges = UIRectEdgeLeft;
    [_redView addGestureRecognizer:screEdgePan];

相关文章

  • IOS-手势事件简述

    在iOS中事件分为三类: 触摸事件:通过触摸。手势进行触发事件(七大手势)运动时间:通过加速器进行触发的事件(摇一...

  • 手势事件基础知识和例子

    点击手势UITapGestureRecognizer 创建手势处理器 //新建轻拍手势事件类并添加手势事件UITa...

  • iOS-手势

    手势使用方法 1.创建手势2.添加手势3.实现手势方法 添加点按手势 代理方法:是否允许接收手指 添加长按手势 添...

  • iOS-手势

    UIResponder UIResponder:是一个响应者(传达者)用来响应用户的触摸屏幕的某些事件 手势 手势...

  • iOS-手势

    iOS中所有的手势操作都继承于UIGestureRecognizer,这个类本身不能直接使用。这个类中定义了这几种...

  • 007-手势事件

    手势事件 ios 手势事件主要有 Tap事件 Tap 事件就是简单的点击事件。 首先放上一个 UIView 用于点...

  • app生命周期、控制器生命周期和事件传递、响应

    生命周期 app生命周期 控制器生命周期 事件 touch事件和手势 触摸事件首先传递到手势上,如果手势识别成功,...

  • iOS· UIGestureRecognizer 与UITouc

    1. 回顾:添加手势的步骤 初始化时为某视图添加手势及事件 实现事件方法 设置手势范围 1.1 初始化时添加手势及...

  • iOS-手势识别

    ios系统提供了一些常用的手势(UIgestureRecognizer的子类),方便我们直接使用。 UIGestu...

  • iOS 开发中的常用手势事件简述

    在开发过程中,我们为了提高用户体验和App的交互友好性,我们通常会在App中添加一些手势,来辅助用户操作.那么这篇...

网友评论

      本文标题:IOS-手势事件简述

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