IOS中各种手势操作实例代码

先看下效果

手势相关的介绍

IOS中手势操作一般是 UIGestureRecognizer 类的几个手势子类去实现,一般我们用到的手势就这么5种:

1、点击  UITapGestureRecognizer

2、平移  UIPanGestureRecognizer

3、缩放  UIPinchGestureRecognizer

4、旋转  UIRotationGestureRecognizer

5、轻扫  UISwipeGestureRecognizer

我们上面这个实例中就用到了上面这5种手势,不过其中 点击与轻扫没有体现出来,只是输出了下日志而已,一会看代码

下面我们来分别介绍下这几种手势

1、UITapGestureRecognizer 点击手势

UITapGestureRecognizer* tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGes:)];
// 点击次数,默认为1,1为单击,2为双击
tapGes.numberOfTapsRequired = 2;

这个点击手势类有一个属性 numberOfTapsRequired 用于设置点击数,就是点击几次才触发这个事件

2、UIPanGestureRecognizer 平移手势

// 平移手势
- (void)initPanGes{
 UIPanGestureRecognizer* panGes = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGes:)];
 [self.imgView addGestureRecognizer:panGes];
}
- (void)panGes:(UIPanGestureRecognizer*)ges{
 // 获取平移的坐标点
 CGPoint transPoint = [ges translationInView:self.imgView];
}

平移手势本身没太多可设置的属性,在平移事件触发手,可以用  translationInView 方法获取当前平移坐标点

3、UIPinchGestureRecognizer 缩放手势

// 缩放手势
- (void)initPinGes{
 UIPinchGestureRecognizer* pinGes = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinGes:)];
 [self.imgView addGestureRecognizer:pinGes];
}
- (void)pinGes:(UIPinchGestureRecognizer*)ges{
 // 缩放
 self.imgView.transform = CGAffineTransformScale(self.imgView.transform, ges.scale, ges.scale);
}

缩放手势在事件里面可以获取 scale 属性,表示当前缩放值

4、UIRotationGestureRecognizer 旋转手势

// 旋转手势
- (void)initRotation{
 UIRotationGestureRecognizer* rotationGes = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGes:)];
 [self.imgView addGestureRecognizer:rotationGes];
}
- (void)rotationGes:(UIRotationGestureRecognizer*)ges{
 // 旋转图片
 self.imgView.transform = CGAffineTransformRotate(self.imgView.transform, ges.rotation);
}

旋转手势在事件里面可以通过获取 rotation 属性获取当前旋转的角度

5、UISwipeGestureRecognizer 轻扫手势

// 轻扫手势
- (void)initSwipeGes{
 // 创建 从右向左 轻扫的手势
 UISwipeGestureRecognizer* swipeLeftGes = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGes:)];
 // 方向,默认是从左往右
 // 最多只能开启一个手势,如果要开启多个就得创建多个手势
 // 监听从右向左的方向
 swipeLeftGes.direction = UISwipeGestureRecognizerDirectionLeft;
 [self.imgView addGestureRecognizer:swipeLeftGes];
}
- (void)swipeGes:(UISwipeGestureRecognizer*)ges{
 // ges.direction方向值
 NSLog(@"%s diection:%lu",__func__,(unsigned long)ges.direction);
}

轻扫手势对象需要设置 direction 属性,默认是只监听从左向右,这是一个枚举值 UISwipeGestureRecognizerDirection

UISwipeGestureRecognizerDirectionRight  从左向右(默认值)
UISwipeGestureRecognizerDirectionLeft   从右向左
UISwipeGestureRecognizerDirectionUp    从下向上
UISwipeGestureRecognizerDirectionDown  从上向下

下面看一下我们上面那个效果图实现代码吧

//
// ViewController.m
// 各种手势操作
//
// Created by xgao on 16/3/24.
// Copyright © 2016年 xgao. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imgView;
@end
@implementation ViewController
- (void)viewDidLoad {
 [super viewDidLoad];
 [self initTapGes];
 [self initPanGes];
 [self initPinGes];
 [self initRotation];
 [self initSwipeGes];
}
// 点击手势
- (void)initTapGes{
 UITapGestureRecognizer* tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGes:)];
 // 点击次数,默认为1,1为单击,2为双击
 tapGes.numberOfTapsRequired = 2;
 tapGes.delegate = self;
 [self.imgView addGestureRecognizer:tapGes];
}
- (void)tapGes:(UITapGestureRecognizer*)ges{
 NSLog(@"%s",__func__);
}
// 平移手势
- (void)initPanGes{
 UIPanGestureRecognizer* panGes = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGes:)];
 panGes.delegate = self;
 [self.imgView addGestureRecognizer:panGes];
}
- (void)panGes:(UIPanGestureRecognizer*)ges{
 // 获取平移的坐标点
 CGPoint transPoint = [ges translationInView:self.imgView];
 // 在之前的基础上移动图片
 self.imgView.transform = CGAffineTransformTranslate(self.imgView.transform, transPoint.x, transPoint.y);
 // 复原,必需复原
 // 每次都清空一下消除坐标叠加
 [ges setTranslation:CGPointZero inView:self.imgView];
 NSLog(@"%s",__func__);
}
// 缩放手势
- (void)initPinGes{
 UIPinchGestureRecognizer* pinGes = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinGes:)];
 pinGes.delegate = self;
 [self.imgView addGestureRecognizer:pinGes];
}
- (void)pinGes:(UIPinchGestureRecognizer*)ges{
 // 缩放
 self.imgView.transform = CGAffineTransformScale(self.imgView.transform, ges.scale, ges.scale);
 // 复原
 // 每次都清空一下消除叠加
 ges.scale = 1;
}
// 旋转手势
- (void)initRotation{
 UIRotationGestureRecognizer* rotationGes = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGes:)];
 rotationGes.delegate = self;
 [self.imgView addGestureRecognizer:rotationGes];
}
- (void)rotationGes:(UIRotationGestureRecognizer*)ges{
 // 旋转图片
 self.imgView.transform = CGAffineTransformRotate(self.imgView.transform, ges.rotation);
 // 复原
 // 每次都清空一下消除叠加
 ges.rotation = 0;
 NSLog(@"%s",__func__);
}
// 轻扫手势
- (void)initSwipeGes{
 // 创建 从右向左 轻扫的手势
 UISwipeGestureRecognizer* swipeLeftGes = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGes:)];
 // 方向,默认是从左往右
 // 最多只能开启一个手势,如果要开启多个就得创建多个手势
 // 监听从右向左的方向
 swipeLeftGes.direction = UISwipeGestureRecognizerDirectionLeft;
 swipeLeftGes.delegate = self;
 [self.imgView addGestureRecognizer:swipeLeftGes];
 // 创建 从下向上 轻扫的手势
 UISwipeGestureRecognizer* swipeUpGes = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeGes:)];
 // 监听从下向上的方向
 swipeUpGes.direction = UISwipeGestureRecognizerDirectionUp;
 swipeUpGes.delegate = self;
 [self.imgView addGestureRecognizer:swipeUpGes];
}
- (void)swipeGes:(UISwipeGestureRecognizer*)ges{
 // ges.direction方向值
 NSLog(@"%s diection:%lu",__func__,(unsigned long)ges.direction);
}
#pragma mark - UIGestureRecognizerDelegate
// 判断是否能触发手势
- (BOOL)gestureRecognizerShouldBegin:(UITapGestureRecognizer *)gestureRecognizer{
 return YES;
}
// 是否允许多手势操作,不是多触摸点
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
 return YES;
}
@end

这里需要注意的有两点:

1、对于 平移、缩放、旋转 这3个手势,我们如果要用它的值去处理的话,要记得复原!复原!复原!这点很重要!重要的事说3遍~~

  平移手势里面我们需要设置 setTranslation:CGPointZero 来复原它的坐标值,不然下一次事件触发这个坐标值会叠加
  缩放手势里面设置 ges.scale = 1 来复原它的缩放值
  旋转手势里面设置 ges.rotation = 0 来复原它的角度值

2、假如我们需要多手势一起用的时候就需要设置下delegate 里面的一个返回参数的方法了

// 是否允许多手势操作,不是多触摸点
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
 return YES;
}

以上所述是小编给大家介绍的IOS中各种手势操作实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!

(0)

相关推荐

  • 利用iOS手势与scrollView代理实现图片的放大缩小

    前言 对于图片拉伸是移动开发中很常见的需求,最近工作中就遇到了利用iOS实现对图片的放大和缩小效果,通过查找资料找到了两种解决方法,分别是用捏合手势和用scrollView的代理方法来实现,下面话不多说,来看看详细的方法介绍吧. 第一种方法:用捏合手势放大缩小 @interface ViewController () @property (strong, nonatomic) IBOutlet UIView *redView; @property (assign, nonatomic) CGFl

  • iOS实现手势密码功能

    手势密码实现 手势密码 一般常常用于金融项目,做的是安全相关的业务.具体实现如下思路,我把它分为view层和逻辑层.我将数据层合并到view层中了,最好是加上数据层用于处理加密的密码和密码的存储 view层 view层主要处理,包括(九个按钮)touchesBegan,touchesMoved,touchesEnded,点与点之间画线,手指滑动画线,画线主要是在drawRect中重绘,提到这里必须不能忘记setNeedsDisplay这个方法.还要记录经过的按钮btnsArray(存放按钮的数组

  • 使用Swift代码实现iOS手势解锁、指纹解锁实例详解

    一.手势密码 1. 1.1.用UIButton组成手势的节点. 1.2.当手指接触屏幕时,调用重写的 touchesBegan:withEvent方法(在touchesBegan里调用setNeedsDisplay,这样就会自动调用drawRect方法). 1.3.当手指在屏幕上滑动时,调用重写的touchesEnded:withEvent方法. 这两个方法执行的操作是一样的:通过locationInView获取 触摸的坐标,然后用 CGRectContainsPoint 判断手指是否经过UIB

  • iOS开发之手势识别实例

    感觉有必要把iOS开发中的手势识别做一个小小的总结.下面会先给出如何用storyboard给相应的控件添加手势,然后在用纯代码的方式给我们的控件添加手势,手势的用法比较简单.和button的用法类似,也是目标 动作回调,话不多说,切入今天的正题. 总共有六种手势识别:轻击手势(TapGestureRecognizer),轻扫手势 (SwipeGestureRecognizer), 长按手势(LongPressGestureRecognizer),  拖动手势(PanGestureRecogniz

  • iOS手势密码的实现方法

    本次讲的手势密码,是在九个按键上实现的,这里讲的是手势密码的基本实现和效果 同样先上效果图 其实就是对画图功能的一个实现,再加上手势操作结合起来. 屏幕宽度高度,方便下面操作,不做解释 #define ScreenHeight [[UIScreen mainScreen] bounds].size.height #define ScreenWidth [[UIScreen mainScreen] bounds].size.width 控制器.m文件 这里的imageView是用来装手势画图之后的

  • IOS中的七种手势小结

    今天为大家介绍一下IOS 的七种手势,手势在开发中经常用到,所以就简单 通俗易懂的说下, 话不多说,直接看代码: // 初始化一个UIimageView UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 300, 300)]; imageView.image = [UIImage imageNamed:@"12.jpg"]; // UIImageView的用户交互是默认关闭的,

  • IOS中多手势之间的冲突和解决办法

    IOS中多手势之间的冲突和解决办法 UIImageView默认是不支持交互的,也就是userInteractionEnabled=NO ,因此要接收触摸事件(手势识别),必须设置userInteractionEnabled=YES(在iOS中UILabel.UIImageView的userInteractionEnabled默认都是NO,UIButton.UITextField.UIScrollView.UITableView等默认都是YES). 在iOS中,如果一个手势A的识别部分是另一个手势

  • IOS中各种手势操作实例代码

    先看下效果 手势相关的介绍 IOS中手势操作一般是 UIGestureRecognizer 类的几个手势子类去实现,一般我们用到的手势就这么5种: 1.点击  UITapGestureRecognizer 2.平移  UIPanGestureRecognizer 3.缩放  UIPinchGestureRecognizer 4.旋转  UIRotationGestureRecognizer 5.轻扫  UISwipeGestureRecognizer 我们上面这个实例中就用到了上面这5种手势,不

  • ios中getTime()的兼容性实例代码

    时间格式为:2017-12-12 12:00:00在苹果上获取时间戳有兼容性问题 需要转换成2017/12/12 12:00:00 才可以正确获取到时间戳 let u = navigator.userAgent; //判断浏览器型号 let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端 let isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X

  • IOS 七种手势操作(拖动、捏合、旋转、点按、长按、轻扫、自定义)详解及实例代码

    IOS 七种手势操作 今天为大家介绍一下IOS 的七种手势,手势在开发中经常用到,所以就简单 通俗易懂的说下, 话不多说,直接看代码: 1.UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性. iOS 系统在 3.2 以后,他提供了一些常用的手势(UIGestureRecognizer 的子类),开发者可以直接使用他们进行手势操作. UIPanGestureRecognizer(拖动) UIPinchGestureRecognizer

  • Django中的cookie与session操作实例代码

    添加cookie: def login(req): if req.method=="POST": uf = UserInfoForm(req.POST) if uf.is_valid(): username = uf.cleaned_data["username"] password = uf.cleaned_data["password"] print username,password users = UserInfo.objects.fil

  • java使用FFmpeg合成视频和音频并获取视频中的音频等操作(实例代码详解)

    FFmpeg是一套可以用来记录.转换数字音频.视频,并能将其转化为流的开源计算机程序. ffmpeg命令参数如下: 通用选项 -L license -h 帮助 -fromats 显示可用的格式,编解码的,协议的... -f fmt 强迫采用格式fmt -I filename 输入文件 -y 覆盖输出文件 -t duration 设置纪录时间 hh:mm:ss[.xxx]格式的记录时间也支持 -ss position 搜索到指定的时间 [-]hh:mm:ss[.xxx]的格式也支持 -title

  • JDBC中resutset接口操作实例详解

    本文主要向大家展示JDBC接口中resutset接口的用法实例,下面我们看看具体内容. 1. ResultSet细节1 功能:封锁结果集数据 操作:如何获得(取出)结果 package com.sjx.a; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import org.junit.Test; //1. next方

  • 往xml中更新节点的实例代码

    往xml中更新节点的实例代码 /* System.out.println("2323"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder docbuilder = factory.newDocumentBuilder(); Document parse = docbuilder .parse(new File("src/ProdQuery.xml&q

  • Yii2中事务的使用实例代码详解

    前言 一般我们做业务逻辑,都不会仅仅关联一个数据表,所以,会面临事务问题. 数据库事务(Database Transaction) ,是指作为单个逻辑工作单元执行的一系列操作,要么完全地执行,要么完全地不执行. 事务处理可以确保除非事务性单元内的所有操作都成功完成,否则不会永久更新面向数据的资源.通过将一组相关操作组合为一个要么全部成功要么全部失败的单元,可以简化错误恢复并使应用程序更加可靠.一个逻辑工作单元要成为事务,必须满足所谓的ACID(原子性.一致性.隔离性和持久性)属性.事务是数据库运

  • jquery 实现复选框的全选操作实例代码

    jquery 实现复选框的全选操作实例代码 最近做了个需求,需要实现列表复选框的全选/取消全选操作,由于之前对这块不是很了解,所以从网上查了一些资料,虽然有各种实现方法,但没找到直接可以套用的.自己琢磨了下,把功能实现,整理如下. 实现细节如有可改进的地方,不吝赐教. 首先是html部分的代码,这里有一个表格,表格里面有一些选项: <div id="list"> <table> <tr><td>选项1<input type=&quo

  • java中的 toString()方法实例代码

    前言: toString()方法 相信大家都用到过,一般用于以字符串的形式返回对象的相关数据. 最近项目中需要对一个ArrayList<ArrayList<Integer>> datas  形式的集合处理. 处理要求把集合数据转换成字符串形式,格式为 :子集合1数据+"#"+子集合2数据+"#"+....+子集合n数据. 举例: 集合数据 :[[1,2,3],[2,3,5]]  要求转成为 "[1,2,3]#[2,3,5]"

随机推荐