.h文件

@property (weak,nonatomic) IBOutlet UILabel *messageLabel;
@property (weak,nonatomic) IBOutlet UILabel *tapsLabel;
@property (weak,nonatomic) IBOutlet UILabel *touchesLabel;

.m文件

-(void)updateLabelsFromTouches:(NSSet *)touches{

//numTaps 连续轻点屏幕多少次  numTouches指用户同时使用几个手指轻点屏幕一次
    NSUInteger numTaps  = [[touches anyObject]tapCount];
    NSString *tapsMessage = [[NSString alloc]initWithFormat:@"%lu taps detected",(unsigned long)numTaps];
    self.messageLabel.text = tapsMessage;
    
    NSUInteger numTouches = [touches count];
    NSString *touchMsg = [[NSString alloc]initWithFormat:@"%lu touches detected",(unsigned long)numTouches];
    self.touchesLabel.text = touchMsg;

}
#pragma mark Touch Event Methods
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

self.messageLabel.text = @"Touches Began";
    
    [self updateLabelsFromTouches:touches];
}

-(void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

self.messageLabel.text = @"Touches Cancelled";
    
    [self updateLabelsFromTouches:touches];

}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

self.messageLabel.text = @"Touches Ended";
    
    [self updateLabelsFromTouches:touches];
}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    self.messageLabel.text = @"Drag Detected";
    
    [self updateLabelsFromTouches:touches];

}

使用touch触摸事件实现轻扫swipe手势的代码如下:

.h文件:

@property (weak,nonatomic) IBOutlet UILabel *swipeLabel;
@property(nonatomic)CGPoint gestureStartPoint;

.m文件:

#import "ViewController.h"

static CGFloat const kMinimumGestureLength = 25;
static CGFloat const kMaximumVariance = 5;

@interface ViewController ()

@end

@implementation ViewController{
    BOOL animate;
}

-(void)eraseText{

self.swipeLabel.text = @"";
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

UITouch *touch = [touches anyObject];
    self.gestureStartPoint = [touch locationInView:self.view];

}
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

//获取用户当前的位置
    UITouch *touch = [touches anyObject];
    CGPoint currentPosition = [touch locationInView:self.view];
    
    CGFloat deltaX = fabs(self.gestureStartPoint.x - currentPosition.x);
    CGFloat deltaY = fabs(self.gestureStartPoint.y - currentPosition.y);
    if (deltaX >= kMinimumGestureLength && deltaY<= kMaximumVariance) {
        
        self.swipeLabel.text = @"Horizontal swipe detected";
        [self performSelector:@selector(eraseText) withObject:nil afterDelay:2];
    }else if(deltaY>= kMinimumGestureLength && deltaX<= kMaximumVariance){
      self.swipeLabel.text = @"Vertical swipe detected";
         [self performSelector:@selector(eraseText) withObject:nil afterDelay:2];
    }

}
@end

touches与ios的手势UIGestureRecognizer的区别就是:

一个是触摸事件;一个手势。UIGestureRecognizer就是对触摸事件的封装。

一、概述

iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由UIResponder而来的如下4种方式:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

但是这种方式甄别不同的手势操作实在是麻烦,需要你自己计算做不同的手势分辨.后来,苹果就给出了一个比较简便的方式,就是使用UIGestureRecognizer

二、UIGestureRecognizer

UIGestureRecognizer基类是一个抽象类,我们主要是使用它的子类(名字包含链接,可以点击跳到ios Developer library,看官方文档):

从名字上我们就能知道, Tap(点击)、Pinch(捏合)、Rotation(旋转)、Swipe(滑动,快速移动,是用于监测滑动的方向的)、Pan (拖移,慢速移动,是用于监测偏移的量的)以及 LongPress(长按)。

其核心就是设置delegate和在需要手势监测的view上使用addGestureRecognizer添加指定的手势监测。

当然要记得在作为delegate的view的头文件加上<UIGestureRecognizerDelegate>。

不过有些手势是关联的,怎么办呢?例如 Tap 与 LongPress、Swipe与 Pan,或是 Tap 一次与Tap 兩次。

手势识别是具有互斥的原则的比如单击和双击,如果它识别出一种手势,其后的手势将不被识别。所以对于关联手势,要做特殊处理以帮助程序甄别,应该把当前手势归结到哪一类手势里面。

比如,单击和双击并存时,如果不做处理,它就只能发送出单击的消息。为了能够识别出双击手势,就需要做一个特殊处理逻辑,即先判断手势是否是双击,在双击失效的情况下作为单击手势处理。使用

[A requireGestureRecognizerToFail:B]函数,它可以指定当A手势发生时,即便A已经滿足条件了,也不会立刻触发会等到指定的手势B确定失败之后才触发。

三、iphone操作手势的大概种类

1.点击(Tap)
点击作为最常用手势,用于按下或选择一个控件或条目(类似于普通的鼠标点击)、

2.拖动(Drag)
拖动用于实现一些页面的滚动,以及对控件的移动功能。

3.滑动(Flick)
滑动用于实现页面的快速滚动和翻页的功能。

4.横扫(Swipe)
横扫手势用于激活列表项的快捷操作菜单

5.双击(Double Tap)
双击放大并居中显示图片,或恢复原大小(如果当前已经放大)。同时,双击能够激活针对文字编辑菜单。

6.放大(Pinch open)
放大手势可以实现以下功能:打开订阅源,打开文章的详情。在照片查看的时候,放大手势也可实现放大图片的功能。

7.缩小(Pinch close)
缩小手势,可以实现与放大手势相反且对应的功能的功能:关闭订阅源退出到首页,关闭文章退出至索引页。在照片查看的时候,缩小手势也可实现缩小图片的功能。

8.长按(Touch &Hold)
在我的订阅页,长按订阅源将自动进入编辑模式,同时选中手指当前按下的订阅源。这时可直接拖动订阅源移动位置。
针对文字长按,将出现放大镜辅助功能。松开后,则出现编辑菜单。
针对图片长按,将出现编辑菜单。

9.摇晃(Shake)
摇晃手势,将出现撤销与重做菜单。主要是针对用户文本输入的。

使用UIGestureRescogize实现横扫(Swipe)手势的代码:

单指横扫:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak,nonatomic) IBOutlet UILabel *swipeLabel;

@end
.m文件:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController{
    BOOL animate;
}

-(void)viewDidLoad{
    [super viewDidLoad];
    
    UISwipeGestureRecognizer *vertical = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(reportVerticalSwipe:)];
    vertical.direction = UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown;
    [self.view addGestureRecognizer:vertical];
    
    
    UISwipeGestureRecognizer *horizontal = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(reportHorizaontalSwipe:)];
    horizontal.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:horizontal];

}
-(void)eraseText{

self.swipeLabel.text = @"";
}
-(void)reportVerticalSwipe:(UIGestureRecognizer *)recognizer{
    self.swipeLabel.text = @"Vertical swipe detected";
    [self performSelector:@selector(eraseText) withObject:nil afterDelay:2];

}
-(void)reportHorizaontalSwipe:(UIGestureRecognizer *)recognizer{
    self.swipeLabel.text = @"Horizontal swipe detected";
    [self performSelector:@selector(eraseText) withObject:nil afterDelay:2];

}
@end

多指横扫代码如下:.h文件一样,.m文件如下

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController{
    BOOL animate;
}

-(void)viewDidLoad{
    [super viewDidLoad];
    
    for (NSUInteger touchConut = 1; touchConut <= 5; touchConut++) {
        
        UISwipeGestureRecognizer *vertical;
        
        vertical = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(reportVerticalSwipe:)];
        vertical.direction = UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown;
        vertical.numberOfTouchesRequired = touchConut;
        [self.view addGestureRecognizer:vertical];
        
         UISwipeGestureRecognizer *horizontal ;
        
       horizontal = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(reportHorizaontalSwipe:)];
        horizontal.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
        horizontal.numberOfTouchesRequired = touchConut;
        [self.view addGestureRecognizer:horizontal];
        
    }

}
-(void)eraseText{

self.swipeLabel.text = @"";
}
-(NSString *)descriptionForTouchCount:(NSUInteger)touchConut{

switch (touchConut) {
        case 1:
            return @"Single";
            
        case 2:
            return @"Double";
        case 3:
            return @"Triple";
        case 4:
            return @"Quadruple";
        case 5:
            return @"Quintuple";
        default:
            return @"";
    }

}
-(void)reportVerticalSwipe:(UIGestureRecognizer *)recognizer{
    self.swipeLabel.text = [NSString stringWithFormat:@"%@Vertical swipe detected",[self descriptionForTouchCount:[recognizer numberOfTouches]]];
    [self performSelector:@selector(eraseText) withObject:nil afterDelay:2];

}
-(void)reportHorizaontalSwipe:(UIGestureRecognizer *)recognizer{
    
      self.swipeLabel.text = [NSString stringWithFormat:@"%@Horizontal swipe detected",[self descriptionForTouchCount:[recognizer numberOfTouches]]];
    [self performSelector:@selector(eraseText) withObject:nil afterDelay:2];

}
@end

捏合和旋转的手势:

#import "ViewController.h"

@interface ViewController ()<UIGestureRecognizerDelegate>

@property (nonatomic,strong)UIImageView *imageView;
@end

@implementation ViewController{
    
    CGFloat scale,previousScale;
    CGFloat rotation,previousRotation;
}

-(void)viewDidLoad{
    [super viewDidLoad];
    
    previousScale = 1;
    UIImage *image = [UIImage imageNamed:@"pic_客服头像@3x"];
    self.imageView = [[UIImageView alloc]initWithImage:image];
    self.imageView.userInteractionEnabled = YES;
    self.imageView.center = self.view.center;
    [self.view addSubview:self.imageView];
    
    
    UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(doPinch:)];
    pinchGesture.delegate = self;
    [self.imageView addGestureRecognizer:pinchGesture];
    
    
    UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(doRote:)];
    rotationGesture.delegate = self;
    [self.imageView addGestureRecognizer:rotationGesture];

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

return YES;

}
-(void)transformImageView{
    CGAffineTransform t = CGAffineTransformMakeScale(scale * previousScale, scale *previousScale);
    t = CGAffineTransformRotate(t, rotation + previousRotation);
    self.imageView.transform = t;

}
-(void)doPinch:(UIPinchGestureRecognizer *)gesture{

scale = gesture.scale;
    [self transformImageView];
    
    if (gesture.state == UIGestureRecognizerStateEnded) {
        previousScale = scale * previousScale;
        scale = 1;
    }

}
-(void)doRote:(UIRotationGestureRecognizer *)gesture{
    rotation = gesture.rotation;
    [self transformImageView];
    
    if (gesture.state == UIGestureRecognizerStateEnded) {
        previousRotation = rotation + previousRotation;
        rotation = 0;
    }
}
@end

点击事件touches与ios的手势UIGestureRecognizer的更多相关文章

  1. [BS-25] IOS中手势UIGestureRecognizer概述

    IOS中手势UIGestureRecognizer概述 一.概述 iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由UIResponder而来的如下4种方式: - (void)touches ...

  2. IOS中手势UIGestureRecognizer

    通常在对视图进行缩放移动等操作的时候我们可以用UIScrollView,因为它里边自带了这些功能,我们要做的就是告诉UIScrollView的几个相关参数就可以了 但是没有实现旋转的手势即UIRota ...

  3. 关于iOS的手势UIGestureRecognizer问题

    typedef NS_ENUM(NSInteger, UIGestureRecognizerState) { UIGestureRecognizerStatePossible, // 尚未识别是何种手 ...

  4. app内嵌vue h5,安卓和ios拦截H5点击事件

    安卓和ios拦截h5点击事件,这个函数事件必须是暴漏在window下的 安卓和ios拦截普通h5函数: <div onclick = "show(),window.android.sh ...

  5. hitTest和pointInside如何响应用户点击事件

    hitTest和pointInside如何响应用户点击事件 处理机制 iOS事件处理,首先应该是找到能处理点击事件的视图,然后在找到的这个视图里处理这个点击事件. 处理原理如下: • 当用户点击屏幕时 ...

  6. iOS开发 解决UITapGestureRecognizer手势与UITableView的点击事件的冲突

    该篇文章摘自我的新浪博客,原文地址为: http://blog.sina.com.cn/s/blog_dcc636350102wavx.html UITableView 拥有属于自己的点击事件,在将一 ...

  7. iOS UITapGestureRecognizer手势和UIButton 以及UITabelView点击事件冲突

    一:在同一个view上加载,UITapGestureRecognizer手势,UIButton 行为,UITabelView点击事件冲突: 二:解决方式: 在UITapGesttureRecogniz ...

  8. IOS开发之功能模块--给任意的UIView添加点击事件

    前言:好久没写博客,今天来一波.我在实际项目开发中,会遇到这样功能需求:我已经搭好了iOS的UI界面,但是很多界面的子View用了UIView,然后这些UIView中用了UILabel和UIImage ...

  9. ios多手势事件

    开发ios应用时我们经常用到多手势来处理事情,如给scrollView增加点击事件,scrollView不能响应view的touch事件,但有时候却要用到多手势事件,那么我们可以给这个scrollVi ...

随机推荐

  1. MongoDB学习笔记——文档操作之查询

    查询文档 使用db.COLLECTION_NAME.findOne()可以查询所有满足条件的第一条数据 预发格式如下: db.COLLECTION_NAME.findOne(<query> ...

  2. Python基本数据结构-元组

  3. 关于CPU Cache:程序猿需要知道的那些

    天下没有免费的午餐,本文转载于:http://cenalulu.github.io/linux/all-about-cpu-cache/ 先来看一张本文所有概念的一个思维导图: 为什么要有CPU Ca ...

  4. [测试] 试用Hadoop 2.2中的HDFS NFS

    Hadoop 2.2中正式启用了hdfs nfs功能,使得hdfs的通用性迈进了一大步.在公司让小朋友搭建了一下,然后我自己进行了一点简单的试验,有一点收获,记录在此. 理论 使用hdfs nfs功能 ...

  5. c++ initialize_list

    看到这么一个东西,可以实现花括号( "{" "}" )初始化容器类. 使用时需包含头文件 #include <initialize_list> 我们 ...

  6. 《TCP/IP详解 卷一》读书笔记-----TCP超时重传

    1.TCP提供的是可靠传输,它通过接收方发送一个确认报文ACK来提供这种可靠性.但是数据报文和确认报文都可能会丢失,所以TCP会给发出的数据报文设置一个时间,如果超时了则进行重传 2.Karn's A ...

  7. 【读书笔记《Android游戏编程之从零开始》】14.游戏开发基础(Bitmap 位图的渲染与操作)

    Bitmap 是图形类,Android 系统支持的图片格式有 png.jpg.bmp 等. 对位图操作在游戏中是很重要的知识点,比如游戏中需要两张除了大小之外其他完全相同的图,那么如果会对位图进行缩放 ...

  8. 最小生成树 2429: [HAOI2006]聪明的猴子

    BZOJ 2429: [HAOI2006]聪明的猴子 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 877  Solved: 566[Submit][ ...

  9. 边工作边刷题:70天一遍leetcode: day 85-2

    Paint Fence 要点: 这题是求number of ways,如果是相邻不相同颜色,那么就trivial了:k*(k-1)^(n-1).所以这里no more than two adjacen ...

  10. codeforces 487A A. Fight the Monster(二分)

    题目链接: A. Fight the Monster time limit per test 1 second memory limit per test 256 megabytes input st ...