一、监听触摸事件的做法

如果想监听一个view上面的触摸事件,之前的做法通常是:先自定义一个view,然后再实现view的touches方法,在方法内部实现具体处理代码
通过touches方法监听view触摸事件,有很明显的几个缺点
(1)必须得自定义view
(2)由于是在view内部的touches方法中监听触摸事件,因此默认情况下,无法让其他外界对象监听view的触摸事件(需要通过代理)
(3)不容易区分用户的具体手势行为
iOS 3.2之后,苹果推出了手势识别功能(Gesture Recognizer),在触摸事件处理方面,大大简化了开发者的开发难度
二、手势识别器
为了完成手势识别,必须借助于手势识别器----UIGestureRecognizer
利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势
UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势
UITapGestureRecognizer(轻拍)
UIPinchGestureRecognizer(捏合,用于缩放)
UIPanGestureRecognizer(拖拽)
UISwipeGestureRecognizer(轻扫)
UIRotationGestureRecognizer(旋转)
UILongPressGestureRecognizer(长按)
UIScreenEdgePanGestureRecognizer(屏幕边缘轻扫)
(一)、轻拍手势
每一个手势识别器的用法都差不多,比如UITapGestureRecognizer的使用步骤如下:
(1)创建手势识别器对象
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
(2)设置手势识别器对象的具体属性
// 连续轻拍2次
tap.numberOfTapsRequired = 2;
// 需要2根手指一起触摸
tap.numberOfTouchesRequired = 2;
(3)添加手势识别器到对应的view上
[self.iconView addGestureRecognizer:tap];
(4)监听手势的触发
[tap addTarget:self action:@selector(tapIconView:)];
属性介绍:
numberOfTouchesRequired //需要多少根手指一起轻拍(默认为1根)
numberOfTapsRequired   //需要轻拍多少下(默认为1)
实现方法
(二)、长按手势
UILongPressGestureRecognizer * longPressGr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressToDo:)];
longPressGr.minimumPressDuration = 1.0;
[self.tableView addGestureRecognizer:longPressGr]; -(void)longPressToDo:(UILongPressGestureRecognizer *)gesture
{
if(gesture.state == UIGestureRecognizerStateBegan)
{
CGPoint point = [gesture locationInView:self.tableView];
NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint:point];
if(indexPath == nil) return ;
//add your code here
}
} 三)、旋转手势
UIRotationGestureRecognizer手势识别器,就像名称一样,这个类能用来监听和捕获旋转的手势,能帮助你创建出更直观的图形用户界面,比如一种场景,当你的应用中有一个展示图片的视图,用户需要通过旋转图片来调整图片的方向。 UIRotationGestureRecognizer这个类有一个rotation的属性,这个属性可以用来设置旋转的方向和旋转的弧度。旋转是从手指的初始位置(UIGestureRecognizerStateBegan)到最终位置(UIGestureRecognizerStateBegan)决定的。 为了对继承自UIView的UI元素进行旋转,你可以将旋转手势识别器的rotation属性传递给CGAffineTransformMakeRotation方法,以制作一个仿射转场 UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateView:)];
[view addGestureRecognizer:rotationGestureRecognizer]; - (void) rotateView:(UIRotationGestureRecognizer *)rotationGestureRecognizer
{
UIView *view = rotationGestureRecognizer.view;
if (rotationGestureRecognizer.state == UIGestureRecognizerStateBegan || rotationGestureRecognizer.state == UIGestureRecognizerStateChanged) {
view.transform = CGAffineTransformRotate(view.transform, rotationGestureRecognizer.rotation);
[rotationGestureRecognizer setRotation:0];
}
}
(四)、平移手势
UIPanGestureRecognizer *panGesture=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanGesture:)];
// [imageView addGestureRecognizer:panGesture]; - (void)panGesture:(UIPanGestureRecognizer *)pan
{
//获取平移手势在视图上的坐标
CGPoint point = [pan locationInView:self.view];
//把视图的中心点确定在平移手势的坐标上,即把视图拖动到平移点
self.mView.center = point;
NSLog(@"%@",NSStringFromCGPoint(point));
} (五)、捏合手势
self.pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinches:)];
[self.myBlackLabel addGestureRecognizer:self.pinchGestureRecognizer]; -(void)handlePinches:(UIPinchGestureRecognizer *)paramSender{
if (paramSender.state == UIGestureRecognizerStateEnded) {
self.currentScale = paramSender.scale;
}else if(paramSender.state == UIGestureRecognizerStateBegan && self.currentScale != 0.0f){
paramSender.scale = self.currentScale;
}
if (paramSender.scale !=NAN && paramSender.scale != 0.0) {
paramSender.view.transform = CGAffineTransformMakeScale(paramSender.scale, paramSender.scale);
}
} (六)、轻扫手势
UISwipeGestureRecognizer *swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
/*Swipes that are performed from right to left are to be detected*/
swipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
/*just one finger needed*/
swipeGestureRecognizer.numberOfTouchesRequired = 1;
/*add it to the view*/
[self.view addGestureRecognizer:swipeGestureRecognizer]; -(void)handleSwipes:(UISwipeGestureRecognizer *)paramSender{
NSLog(@"swipe to left");
} 八)、同时触发两个view的手势
手势之间是互斥的,如果你想同时触发蛇和龙的view,那么需要实现协议
(1)、遵循协议。
(2)、同意添加多个手势。
(3)、把self作为代理设置给手势

iOS 七大手势之轻拍,长按,旋转手势识别器方法-赵小波的更多相关文章

  1. iOS 七大手势之轻拍,长按,旋转手势识别器方法

    一.监听触摸事件的做法   如果想监听一个view上面的触摸事件,之前的做法通常是:先自定义一个view,然后再实现view的touches方法,在方法内部实现具体处理代码 通过touches方法监听 ...

  2. 在imageView依次加入7个手势, 1.点击哪个button,往imageView上加入哪个手势.(保证视图上仅仅有一个手势). 2.轻拍:点击视图切换美女图片.(imageView上首先展示的美女

    // // ControlView.h // HomeworkGestureRecognizer // // Created by lanouhn on 14-8-27. // Copyright ( ...

  3. ios 类别和扩展-赵小波

    类别 @interface ClassName ( CategoryName ) // method declarations @end Category在iOS开发中使用非常频繁.尤其是在为系统类进 ...

  4. iOS七大手势之(平移、捏合、轻扫、屏幕边缘轻扫)手势识别器方法

    使用手势很简单,分为两步: 创建手势实例.当创建手势时,指定一个回调方法,当手势开始,改变.或结束时,回调方法被调用. 添加到需要识别的View中.每个手势只对应一个View,当屏幕触摸在View的边 ...

  5. IOS 手势-轻点、触摸、手势、事件

    1.概念 手势是从你用一个或多个手指接触屏幕时开始,直到手指离开屏幕为止所发生的所有事件.无论手势持续多长时间,只要一个或多个手指仍在屏幕上,这个手势就存在. 触摸是指把手指放到IOS设备的屏幕上,从 ...

  6. iOS开发-轻点、触摸和手势

    一.响应者链 以UIResponder作为超类的任何类都是响应者.UIView和UIControl是UIReponder的子类,因此所有视图和所有控件都是响应者. 初始相应器事件首先会传递给UIApp ...

  7. iOS七大手势识别

    也没有什么好说的,方法都差不多,只要记得当你想要同时实现两个或多个手势的话,要遵守<UIGestureRecognizerDelegate>协议,闲言休叙,直接上代码: #import & ...

  8. [OC笔记]@property之个人理解,大神轻拍

    /** * 一个简单的对象 * * @author suzhen * */ public class SimpleObjcet { /** * 声明一个age字段 */ private Object ...

  9. iOS开发拓展篇—xib中关于拖拽手势的潜在错误

    iOS开发拓展篇—xib中关于拖拽手势的潜在错误 一.错误说明 自定义一个用来封装工具条的类 搭建xib,并添加一个拖拽的手势. 主控制器的代码:加载工具条 封装工具条以及手势拖拽的监听事件 此时运行 ...

随机推荐

  1. python_自动查找指定目录下的文件或目录的方法

    代码如下 import os def find_file(search_path, file_type="file", filename=None, file_startswith ...

  2. CentOS 7 如何清空文件内容

    https://www.cnblogs.com/zqifa/p/linux-vim-4.html 方法1.在非编辑状态下使用快捷键gg跳至首行头部,再使用dG即可清空,或 输入"%d&quo ...

  3. webSocket 使用 HttpSession 的数据配置与写法

    1.前言 webSoket 无法获取 HttpSession  ,使用就更谈不上了 !!! 2解决过程 使用   configurator  注入即可 (1) 配置一个类 1 package cn.c ...

  4. react中prop-types的使用

    什么是prop-types?prop代表父组件传递过来的值,types代表类型.简单来说就是用来校验父组件传递过来值的类型 import PropTypes from 'prop-types'; To ...

  5. 详解Scrapy的命令行工具

    接触过Scrapy的人都知道,我们很多操作是需要借助命令行来执行的,比如创建项目,运行爬虫等.所以了解和掌握这些命令对于scrapy的学习是很有帮助的! Scrapy 命令 首先,在scrapy命令中 ...

  6. 服务器表单字符串转化Vue表单挂在到对应DOM节点

    今天在项目开发中,遇到从后端返回的vue文件(包含template,js,css)的文件,试过用v-html解析文件,渲染到页面,但是无法渲染,后来去查了一堆资料,自己写了一个全局方法来解析这类文件 ...

  7. 【记录一个问题】android下opencl中的event.getProfilingInfo()测速时间并不准确

    使用了类似的代码来做android下opencl的时间测试: cl::CommandQueue queue(context, devices[0], CL_QUEUE_PROFILING_ENABLE ...

  8. 【webpack4.0】---webpack的基本使用(二)

    一.什么是plugins plugins可以使webpack在运行到某个时刻的时候,帮你做一些事情,类似于生命周期一样 plugins,它就是一个扩展器,它丰富了wepack本身,针对是loader结 ...

  9. 004 Linux 揭开神器 vim 面纱

    01 开篇初识 vim vim 功能吊炸天,但我们掌握一些常用的命令即可应对日常的使用了,不记流水账! Linux 中最常用的编辑器是什么? vim ! vi 跟 vim 啥区别? vim 就是 vi ...

  10. 执行df hang住

    突然有一天发现df执行卡住了,一直不显示结果. $ df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 221G 100G 121G ...