A.系统提供的手势识别器
 
1.敲击手势 UITapGestureRecognizer
numberOfTapsRequired: 敲击次数
numberOfTouchesRequired: 同时敲击触碰数(手指数)
 - (void) testTap {
// 创建手势识别器
UITapGestureRecognizer *tapRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRun:)];
tapRec.numberOfTapsRequired = ; // 触发需要点击的次数
tapRec.numberOfTouchesRequired = ; // 触发需要同时点击的点数目 // 配置手势识别器到控件
[self.hvwView addGestureRecognizer: tapRec];
} /** tap手势的事件处理方法 */
- (void) tapRun:(UITapGestureRecognizer *) tapRec {
NSLog(@"tapRun");
}
2.长按手势 UILongPressGestureRecognizer
minimumPressDuration:长按生效时间
numberOfTouchesRequired:需要的同时点击数
numberOfTapsRequired:需要的点击次数
allowableMovement:长按有效移动范围(从点击开始,长按移动的允许范围)
ps:移动的时候会不断调用目标方法
 - (void) testLongPress {
UILongPressGestureRecognizer *longRec = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressRun:)]; longRec.minimumPressDuration = ; // 长按生效时间
longRec.allowableMovement = ; // 长按允许移动范围,单位:px [self.hvwView addGestureRecognizer:longRec];
}
 
3.轻扫手势 UISwipeGestureRecognizer
direction:扫的方向 上下左右
 typedef enum {
UISwipeGestureRecognizerDirectionRight = << ,
UISwipeGestureRecognizerDirectionLeft = << ,
UISwipeGestureRecognizerDirectionUp = << ,
UISwipeGestureRecognizerDirectionDown = <<
} UISwipeGestureRecognizerDirection;
 
 - (void) testSwipe {
UISwipeGestureRecognizer *swipeRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRun:)];
swipeRec.direction = UISwipeGestureRecognizerDirectionRight; // 轻扫的方向 [self.hvwView addGestureRecognizer:swipeRec];
}
4.捏合手势 UIPinchGestureRecognizer
scale: 捏合的距离
 
 - (void) testPinch {
UIPinchGestureRecognizer *rec = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchRun:)]; [self.hvwView addGestureRecognizer:rec];
} - (void) pinchRun:(UIPinchGestureRecognizer *) rec {
rec.view.transform = CGAffineTransformScale(rec.view.transform, rec.scale, rec.scale); // 一定要复位!!!不然按住捏合会不断叠加倍数
rec.scale = ;
}
 
 
 
5.旋转手势 UIRotationGestureRecognizer
rotation: 旋转了的角度
 - (void) testRotation {
UIRotationGestureRecognizer *rotationRec = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationRun:)]; [self.hvwView addGestureRecognizer:rotationRec];
} - (void) rotationRun:(UIRotationGestureRecognizer *) rec {
rec.view.transform = CGAffineTransformRotate(rec.view.transform, rec.rotation); // 一定要复位!!!不然会在按住旋转的时候会不断叠加rotation
rec.rotation = ;
}
 
 
6.拖曳手势 UIPanGestureRecognizer
相对始点拖曳距离:[pan translationInView:pan.view]
 - (void) testPan {
UIPanGestureRecognizer *rec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panRun:)]; [self.hvwView addGestureRecognizer:rec];
} - (void) panRun:(UIPanGestureRecognizer *) rec {
// 取得拖曳距离
CGPoint movedDistance = [rec translationInView:rec.view];
CGPoint viewCenter = rec.view.center;
viewCenter.x += movedDistance.x;
viewCenter.y += movedDistance.y;
rec.view.center = viewCenter; // 复位拖曳距离
[rec setTranslation:CGPointZero inView:rec.view];
}
 
 
 
 
B.一般情况只能使用一个识别器,使用代理设置才能同时使用多个识别器

 
1.遵守协议:
 @interface ViewController () <UIGestureRecognizerDelegate>
 
2.给需要同时进行识别的手势识别器设置代理
 rec.delegate = self;
 
3.实现手势识别器同时使用方法
 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

     // 任何两种手势识别器都能同时使用
return YES;
}
 
 //
// ViewController.m
// GestureRecognizerTest
//
// Created by hellovoidworld on 15/1/13.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "ViewController.h" @interface ViewController () <UIGestureRecognizerDelegate> @property (weak, nonatomic) IBOutlet UIView *hvwView; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. // [self testTap];
// [self testLongPress];
// [self testSwipe];
[self testRotation];
[self testPinch];
[self testPan];
} - (void) testTap {
// 创建手势识别器
UITapGestureRecognizer *tapRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRun:)];
tapRec.numberOfTapsRequired = ; // 触发需要点击的次数
tapRec.numberOfTouchesRequired = ; // 触发需要同时点击的点数目 // 配置手势识别器到控件
[self.hvwView addGestureRecognizer: tapRec];
} /** tap手势的事件处理方法 */
- (void) tapRun:(UITapGestureRecognizer *) tapRec {
NSLog(@"tapRun");
} - (void) testLongPress {
UILongPressGestureRecognizer *longRec = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressRun:)]; longRec.minimumPressDuration = ; // 长按生效时间
longRec.allowableMovement = ; // 长按允许移动范围,单位:px [self.hvwView addGestureRecognizer:longRec];
} - (void) longPressRun:(UILongPressGestureRecognizer *) rec {
NSLog(@"longPress");
} - (void) testSwipe {
UISwipeGestureRecognizer *swipeRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRun:)];
swipeRec.direction = UISwipeGestureRecognizerDirectionRight; // 轻扫的方向 [self.hvwView addGestureRecognizer:swipeRec];
} - (void) swipeRun:(UISwipeGestureRecognizer *) rec {
NSLog(@"swipe");
} - (void) testRotation {
UIRotationGestureRecognizer *rotationRec = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationRun:)];
rotationRec.delegate = self; [self.hvwView addGestureRecognizer:rotationRec];
} - (void) rotationRun:(UIRotationGestureRecognizer *) rec {
rec.view.transform = CGAffineTransformRotate(self.hvwView.transform, rec.rotation); // 一定要复位!!!不然会在按住旋转的时候会不断叠加rotation
rec.rotation = ;
} - (void) testPinch {
UIPinchGestureRecognizer *rec = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchRun:)];
rec.delegate = self; [self.hvwView addGestureRecognizer:rec];
} - (void) pinchRun:(UIPinchGestureRecognizer *) rec {
rec.view.transform = CGAffineTransformScale(rec.view.transform, rec.scale, rec.scale); // 一定要复位!!!不然按住捏合会不断叠加倍数
rec.scale = ;
} - (void) testPan {
UIPanGestureRecognizer *rec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panRun:)];
rec.delegate = self; [self.hvwView addGestureRecognizer:rec];
} - (void) panRun:(UIPanGestureRecognizer *) rec {
// 取得拖曳距离
CGPoint movedDistance = [rec translationInView:rec.view];
CGPoint viewCenter = rec.view.center;
viewCenter.x += movedDistance.x;
viewCenter.y += movedDistance.y;
rec.view.center = viewCenter; // 复位拖曳距离
[rec setTranslation:CGPointZero inView:rec.view];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { // 任何两种手势识别器都能同时使用
return YES;
} @end
 
#mark:
因为使用的是transform形变进行缩放、旋转,所以再进行拖曳的时候是不能正常进行的。

[iOS UI进阶 - 3.2] 手势识别器UIGestureRecognizer的更多相关文章

  1. iOS UI进阶-6.0 手势

    给每个页面添加手势,只需要统一设置不是根控制器的页面,都增加手势.需要自定义导航控制器 1.继承代理 @interface BSNavigationController ()<UIGesture ...

  2. [iOS UI进阶 - 5.0] 手势解锁Demo

    A.需求 1.九宫格手势解锁 2.使用了绘图和手势事件   code source: https://github.com/hellovoidworld/GestureUnlockDemo     B ...

  3. [iOS UI进阶 - 3.1] 触摸事件的传递

    A.事件的产生和传递 发生触摸事件后,系统会将该事件加入到一个由UIApplication管理的事件队列中UIApplication会从事件队列中取出最前面的事件,并将事件分发下去以便处理,通常,先发 ...

  4. [iOS UI进阶 - 0] Quiartz2D

    A.简介 1. 需要掌握的 drawRect:方法的使用 常见图形的绘制:线条.多边形.圆 绘图状态的设置:文字颜色.线宽等 图形上下文状态的保存与恢复 图形上下文栈 1.基本图形绘制* 线段(线宽. ...

  5. [iOS UI进阶 - 3.0] 触摸事件的基本处理

    A.需要掌握和练习的 1.介绍事件类型2.通过按钮的事件处理引出view的事件处理3.响应者对象 --> UIResponder --> UIView4.view的拖拽* 实现触摸方法,打 ...

  6. iOS UI进阶-1.0 Quartz2D

    概述 Quartz 2D是一个二维绘图引擎,同时支持iOS和Mac系统.Quartz 2D能完成的工作: 绘制图形 : 线条\三角形\矩形\圆\弧等 绘制文字 绘制\生成图片(图像) 读取\生成PDF ...

  7. [iOS UI进阶 - 6.1] 核心动画CoreAnimation

    A.基本知识 1.概念 Core Animation是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍,使用它需要先添加QuartzCore.framework和引入对 ...

  8. [iOS UI进阶 - 6.0] CALayer

    A.基本知识 1.需要掌握的 CALayer的基本属性 CALayer和UIView的关系 position和anchorPoint的作用   2.概念 在iOS中,你能看得见摸得着的东西基本上都是U ...

  9. [iOS UI进阶 - 2.3] 彩票Demo v1.3

    A.需求 真机调试 "关于”模块 存储开关状态 打电话.发短信 应用评分 打开其他应用 cell 在iOS6 和 iOS7的适配 block的循环引用 屏幕适配 code source:   ...

随机推荐

  1. launch genymotion simulator from command line

    Command to launch genymotion headless - player --vm-name Nexus_4 if player is not already added to p ...

  2. HDU 1166 敌兵布阵 (线段树 单点更新)

    题目链接 线段树掌握的很差,打算从头从最简单的开始刷一波, 嗯..就从这个题开始吧! #include <iostream> #include <cstdio> #includ ...

  3. 关于Hibernate中的Configuration

    Hibernate中,关于从 Configuration中建立一个SessionFactory常用的可以有两种方法,一种是为Configuration提供hibernate.cfg.xml配置文件,还 ...

  4. Codeforces Round #273 (Div. 2)

    A. Initial Bet 题意:给出5个数,判断它们的和是否为5的倍数,注意和为0的情况 #include<iostream> #include<cstdio> #incl ...

  5. Android PRODUCT_COPY_FILES 自动拷贝文件

    /********************************************************************** * Android PRODUCT_COPY_FILES ...

  6. Android service binder aidl 关系

    /********************************************************************************** * Android servic ...

  7. 【 D3.js 高级系列 — 10.0 】 思维导图

    思维导图的节点具有层级关系和隶属关系,很像枝叶从树干伸展开来的形状.在前面讲解布局的时候,提到有五个布局是由层级布局扩展来的,其中的树状图(tree layout)和集群图(cluster layou ...

  8. 【LCS,LIS】最长公共子序列、单调递增最长子序列

    单调递增最长子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 求一个字符串的最长递增子序列的长度如:dabdbf最长递增子序列就是abdf,长度为4   输入 ...

  9. HTML5实现扫描识别二维码/生成二维码

    扫描识别二维码 思路: 1. 操作摄像头,获取图片.HTML5 WEBRTC的navigator.getUserMedia方法去实时获取摄像头资源.  2. 利用canvas使用相关算法分析图片识别图 ...

  10. POJ 1068 Parencodings

    Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 24932   Accepted: 14695 De ...