先看下效果

手势相关的介绍

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 = ;

这个点击手势类有一个属性 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 = ;
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 = ;
} // 旋转手势
- (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 = ; 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各种手势操作实例的更多相关文章

  1. ios的手势操作之UIGestureRecognizer浅析

    转载地址:http://blog.csdn.net/likendsl/article/details/7554150 每一个手势的实现例子,可参考下面网址:http://www.cnblogs.com ...

  2. 【转】 ios的手势操作之UIGestureRecognizer浅析

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

  3. Android学习指南之三十八:Android手势操作编程[转]

    手势操作在我们使用智能设备的过程中奉献了不一样的体验.Android开发中必然会进行手势操作方面的编程.那么它的原理是怎样的呢?我们如何进行手势操作编程呢? 手势操作原理 首先,在Android系统中 ...

  4. iOS 手势操作:拖动、捏合、旋转、点按、长按、轻扫、自定义

    1.UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性. iOS 系统在 3.2 以后,他提供了一些常用的手势(UIGestureReco ...

  5. iOS手势操作,拖动,轻击,捏合,旋转,长按,自定义(http://www.cnblogs.com/huangjianwu/p/4675648.html)

    1.UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性. iOS 系统在 3.2 以后,他提供了一些常用的手势(UIGestureReco ...

  6. iOS项目之“返回”手势操作相关

    在程序中,总会设置“返回”按钮,但不可能在每一个控制器中都去设置一次“返回”按钮,那如何设置全局的“返回”按钮呢? 首先自定义一个导航控制器,在tabBarController中添加子控制器时,使用这 ...

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

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

  8. 点击事件touches与ios的手势UIGestureRecognizer

    .h文件 @property (weak,nonatomic) IBOutlet UILabel *messageLabel;@property (weak,nonatomic) IBOutlet U ...

  9. ios各种手势,很有意思

    转自http://blog.csdn.net/likendsl/article/details/7554150 这哥们很厉害的 一.概述 iPhone中处理触摸屏的操作,在3.2之前是主要使用的是由U ...

随机推荐

  1. ODBC

    ODBC是80年代末90年代初出现的技术,它为编写关系数据库的客户软件提供了统一的接口.ODBC只提供单一的API,可用于处理不同数据库的客户应用程序.使用ODBC API的应用程序可以与任何具有OD ...

  2. 修改WebView

    http://jwdev.cn/2015/09/28/use-javascript-to-delete-web-element/ 

  3. iOS之网络数据下载和JSON解析

    iOS之网络数据下载和JSON解析 简介 在本文中笔者将要给大家介绍IOS中如何利用NSURLconnection从网络上下载数据以及如何解析下载下来的JSON数据格式,以及如何显示数据和托图片的异步 ...

  4. spring boot项目发布tomcat容器(包含发布到tomcat6的方法)

    spring boot因为内嵌tomcat容器,所以可以通过打包为jar包的方法将项目发布,但是如何将spring boot项目打包成可发布到tomcat中的war包项目呢? 1. 既然需要打包成wa ...

  5. ASP.NET使用WebApi接口实现与Android客户端的交互(图片或字符串的接收与回传)

    最近在使用WebApi   做下记录 //此接口实现接收Android客户端上传的JSON格式的信息,并返回"nihao"字符串 [Route("ReceiveData& ...

  6. 深度了解Android 7.0 ,你准备好了吗?

    作者:Redyan, 腾讯移动客户端开发工程师 商业转载请联系腾讯WeTest获得授权,非商业转载请注明出处. 原文链接:http://wetest.qq.com/lab/view/288.html ...

  7. ionic接入广告

    一.获取Admob phonegap 广告插件(cordova 广告平台插件) 在cordova 和phonegap等html5手机应用里面展示Admob,百度移动联盟,广点通广告需要Cordova ...

  8. 深入理解ajax系列第四篇——请求实例

    前面的话 在使用ajax的过程中,常用的请求方式是GET和POST两种.本文将以实例的形式来详细说明这两种请求方式 GET GET是最常见的请求类型,最常用于向服务器查询某些信息.必要时,可以将查询字 ...

  9. 谈谈事件对象-event

    JavaScript 中的事件对象(event) 当我们每次触发一种事件(如点击事件),我们会在回调函数中传入事件对象event.今天就来来谈谈. 1.当我们想判断当前事件是我们想要的事件类型时,可以 ...

  10. JavaScript中的文档模式和严格模式

    JavaScript中的文档模式和严格模式 语法模式有普通模式和严格模式两种 普通模式:正常的JavaScript语法拼写以及代码编写(相对于严格模式存在着语法上的不严谨),尽可能的识别错误以及不规范 ...