zoukankan      html  css  js  c++  java
  • IOS触摸事件和手势识别

    IOS触摸事件和手势识别

     

    目录

    • 概述
    • 触摸事件
    • 手势识别

     

    概述

    为了实现一些新的需求,我们常常需要给IOS添加触摸事件和手势识别

     

    触摸事件

    触摸事件的四种方法

    -(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  系统由于某原因取消触摸事件时所调用的方法

    获得触摸位置的方法

    for(UITouch *t in touches){

    CGPoint touchPosition = [t locationInView:self.view];

    NSLog(@"%f,%f",touchPosition.x,touchPosition.y);  

    }

      

    手势识别

    手势种类

    UITapGestureRecognizer轻触

    UISwipeGestureRecognizer很快的滑动

    UIPanGestureRecognizer拖动

    UIPinchGestureRecognizer两个手指头捏或放

    UIRotationGestureRecognizer手指方向操作

    UILongPressGestureRecognizer长按

    UITapGestureRecognizer轻触

    初始化

    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];

    [view addGestureRecognizer:tapGR];

    常用方法

    [self setNumberOfTapsRequired:numbers];

    [self setNumberOfTouchesRequired:number2];

    其他方法可以参考UIGestureRecognizer文档

    UITapGestureRecognizer的响应方法

    -(void)tapAction:(UITapGestureRecognizer *)sender{

    //Tap拿起的时候

    if(tapGR.state == UIGestureRecognizerStateEnded){

    }

    //Tap按下的时候

    if(tapGR.state == UIGestureRecognizerStateBegan){

    }

    //等等其他状态

    }

    获得UITapGestureRecognizer按下的位置

    CGPoint point = [sender locationInView:view];

     

     

  • 相关阅读:
    初听余杭...
    生命里走了一位花儿,同时却遇到了10年未见的老同学
    两个馒头过中秋
    周末粉红色的回忆
    jQuery图片剪裁插件 Jcrop
    上周六出去烤肉随便拍了几张
    一周学会Mootools 1.4中文教程:(7)汇总收尾
    30天学会 MooTools 教学(4): 函数和MooTools
    用Mootools寫的一个类似facebook的弹出对话框
    用Mootools获得操作索引的两种方法
  • 原文地址:https://www.cnblogs.com/IOS-Developer/p/4118571.html
Copyright © 2011-2022 走看看