在IOS中手势可以让用户有很好的体验,因此我们有必要去了解一下手势。

(在设置手势是有很多值得注意的地方)

*是需要设置为Yes的点击无法响应*

*要把手势添加到所需点击的View,否则无法响应*

手势共有六种,下面我会分开介绍。

点击手势

//
// ViewController.m
// CX-手势详解
//
// Created by ma c on 16/3/24.
// Copyright © 2016年 xubaoaichiyu. All rights reserved.
// #import "ViewController.h" @interface ViewController () @property (nonatomic, strong) UIImageView * imageView; @end @implementation ViewController
#pragma mark - set_and_get
-(UIImageView *)imageView{
if (!_imageView) { _imageView = [[UIImageView alloc]init]; UIImage * image = [UIImage imageNamed:@"nvshen.jpg"]; _imageView.bounds = (CGRect){CGPointZero,image.size}; _imageView.center = self.view.center;
//交互一定要设置为YES 否则无法实现手势
_imageView.userInteractionEnabled = YES; _imageView.image = image; }
return _imageView;
}
- (void)viewDidLoad {
[super viewDidLoad]; [self.view addSubview:self.imageView]; //点击手势 UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doAction:)]; //点击一下生效
tap.numberOfTapsRequired = ; UITapGestureRecognizer * tapNew = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doAction:)]; //点击两下生效
tapNew.numberOfTapsRequired = ; //在imageView上添加手势
[self.imageView addGestureRecognizer:tap];
[self.imageView addGestureRecognizer:tapNew]; //当点击两下生效时,使点击一下失效
[tap requireGestureRecognizerToFail:tapNew]; } -(void)doAction:(UITapGestureRecognizer *)tap{ if (tap.numberOfTapsRequired == ) {
NSLog(@"点击一下");
}else if(tap.numberOfTapsRequired == ){
NSLog(@"点击两下");
} } @end

拖动手势

//
// ViewController.m
// CX-手势详解
//
// Created by ma c on 16/3/24.
// Copyright © 2016年 xubaoaichiyu. All rights reserved.
// #import "ViewController.h" @interface ViewController () @property (nonatomic, strong) UIImageView * imageView; @end @implementation ViewController
#pragma mark - set_and_get
-(UIImageView *)imageView{
if (!_imageView) { _imageView = [[UIImageView alloc]init]; UIImage * image = [UIImage imageNamed:@"nvshen.jpg"]; _imageView.bounds = (CGRect){CGPointZero,image.size}; _imageView.center = self.view.center;
//交互一定要设置为YES 否则无法实现手势
_imageView.userInteractionEnabled = YES; _imageView.image = image; }
return _imageView;
}
- (void)viewDidLoad {
[super viewDidLoad]; [self.view addSubview:self.imageView]; //拖动手势 UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(doAction:)]; [self.view addGestureRecognizer:pan]; } -(void)doAction:(UIPanGestureRecognizer *)pan{
//获取偏移量
CGPoint point = [pan translationInView:self.imageView]; //通过改变self。imageView的Center来实现拖动
self.imageView.center = CGPointMake(self.imageView.center.x + point.x
, self.imageView.center.y + point.y); //复位 如果不进行复位 会在改变的基础上改变 从而使效果不对
[pan setTranslation:CGPointZero inView:self.imageView]; } @end

长按手势

//
// ViewController.m
// CX-手势详解
//
// Created by ma c on 16/3/24.
// Copyright © 2016年 xubaoaichiyu. All rights reserved.
// #import "ViewController.h" @interface ViewController () @property (nonatomic, strong) UIImageView * imageView; @end @implementation ViewController
#pragma mark - set_and_get
-(UIImageView *)imageView{
if (!_imageView) { _imageView = [[UIImageView alloc]init]; UIImage * image = [UIImage imageNamed:@"nvshen.jpg"]; _imageView.bounds = (CGRect){CGPointZero,image.size}; _imageView.center = self.view.center;
//交互一定要设置为YES 否则无法实现手势
_imageView.userInteractionEnabled = YES; _imageView.image = image; }
return _imageView;
}
- (void)viewDidLoad {
[super viewDidLoad]; [self.view addSubview:self.imageView]; //长按手势 UILongPressGestureRecognizer * longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(doAction:)]; [self.imageView addGestureRecognizer:longPress]; } -(void)doAction:(UILongPressGestureRecognizer *)longPress{ if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"开始");
}
else if (longPress.state == UIGestureRecognizerStateEnded){
NSLog(@"结束");
} } @end

轻扫手势

//
// ViewController.m
// CX-手势详解
//
// Created by ma c on 16/3/24.
// Copyright © 2016年 xubaoaichiyu. All rights reserved.
// #import "ViewController.h" @interface ViewController () @property (nonatomic, strong) UIImageView * imageView; @end @implementation ViewController
#pragma mark - set_and_get
-(UIImageView *)imageView{
if (!_imageView) { _imageView = [[UIImageView alloc]init]; UIImage * image = [UIImage imageNamed:@"nvshen.jpg"]; _imageView.bounds = (CGRect){CGPointZero,image.size}; _imageView.center = self.view.center;
//交互一定要设置为YES 否则无法实现手势
_imageView.userInteractionEnabled = YES; _imageView.image = image; }
return _imageView;
}
- (void)viewDidLoad {
[super viewDidLoad]; [self.view addSubview:self.imageView]; //轻扫手势 UISwipeGestureRecognizer * swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(doAction:)];
//需要设置 默认为右
/*
默认是UISwipeGestureRecognizerDirectionRight。所需的方向刷。可指定多个方向是否会导致相同的行为(例如,UITableView滑动删除)
*/
swipe.direction = UISwipeGestureRecognizerDirectionLeft; [self.imageView addGestureRecognizer:swipe]; } -(void)doAction:(UISwipeGestureRecognizer *)swipe{ if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"左");
}
else if (swipe.direction == UISwipeGestureRecognizerDirectionRight){
NSLog(@"右");
}
else if (swipe.direction == UISwipeGestureRecognizerDirectionDown){
NSLog(@"下");
}
else if (swipe.direction == UISwipeGestureRecognizerDirectionUp){
NSLog(@"上");
}
} @end

捏合手势

(在捏合和旋转手势中我们需要一些操作)

*按住option 在触碰到触摸板的时候会出现模拟出现的两根手指*

*如果你所操作的view不在两个触摸点的位置,可以按住shift进行移动*

*当进行捏合旋转的时候,一定要把触摸板按下,才可进行操作*

//
// ViewController.m
// CX-手势详解
//
// Created by ma c on 16/3/24.
// Copyright © 2016年 xubaoaichiyu. All rights reserved.
// #import "ViewController.h" @interface ViewController () @property (nonatomic, strong) UIImageView * imageView; @end @implementation ViewController
#pragma mark - set_and_get
-(UIImageView *)imageView{
if (!_imageView) { _imageView = [[UIImageView alloc]init]; UIImage * image = [UIImage imageNamed:@"nvshen.jpg"]; _imageView.bounds = (CGRect){CGPointZero,image.size}; _imageView.center = self.view.center;
//交互一定要设置为YES 否则无法实现手势
_imageView.userInteractionEnabled = YES; _imageView.image = image; }
return _imageView;
}
- (void)viewDidLoad {
[super viewDidLoad]; [self.view addSubview:self.imageView]; //捏合手势 UIPinchGestureRecognizer * pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(doAction:)]; [self.imageView addGestureRecognizer:pinch]; } -(void)doAction:(UIPinchGestureRecognizer *)pinch{
//持续改变
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);
//复位
pinch.scale = ; } @end

旋转手势

//
// ViewController.m
// CX-手势详解
//
// Created by ma c on 16/3/24.
// Copyright © 2016年 xubaoaichiyu. All rights reserved.
// #import "ViewController.h" @interface ViewController () @property (nonatomic, strong) UIImageView * imageView; @end @implementation ViewController
#pragma mark - set_and_get
-(UIImageView *)imageView{
if (!_imageView) { _imageView = [[UIImageView alloc]init]; UIImage * image = [UIImage imageNamed:@"nvshen.jpg"]; _imageView.bounds = (CGRect){CGPointZero,image.size}; _imageView.center = self.view.center;
//交互一定要设置为YES 否则无法实现手势
_imageView.userInteractionEnabled = YES; _imageView.image = image; }
return _imageView;
}
- (void)viewDidLoad {
[super viewDidLoad]; [self.view addSubview:self.imageView]; //旋转手势 UIRotationGestureRecognizer * rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(doAction:)]; [self.imageView addGestureRecognizer:rotation]; } -(void)doAction:(UIRotationGestureRecognizer *)rotation{
//持续改变
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation.rotation);
//复位
rotation.rotation = ; } @end

有一点值得注意的是,旋转手势和捏合手势是不可以同时操作的,想要同时操作可以通过代理实现,如下。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;

在上面的代码实现时返回YES即可。

IOS 手势详解的更多相关文章

  1. 【转】IOS AutoLayout详解(三)用代码实现(附Demo下载)

    转载自:blog.csdn.net/hello_hwc IOS SDK详解 前言: 在开发的过程中,有时候创建View没办法通过Storyboard来进行,又需要AutoLayout,这时候用代码创建 ...

  2. IOS SDK详解

    来源:http://blog.csdn.net/column/details/huangwenchen-ios-sdk.html?page=1#42803301 博客专栏>移动开发专栏>I ...

  3. iOS路由详解

    本文如题,路由详解,注定是一篇详细解释iOS路由原理及使用的文章,由于此时正在外地出差,无法详细一一写出,只能不定时的补充. 一.什么是iOS路由 路由一词来源于路由器,可以实现层级之间消息转发的功能 ...

  4. IOS SizeClasses 详解

    SizeClasses 详解 iOS 8在应用界面的可视化设计上添加了一个新的特性-Size Classes.对于任何设备来说,界面的宽度和高度都只分为三种描述:紧凑,任意和宽松.这样开发者便可以无视 ...

  5. iOS模式详解—「runtime面试、工作」看我就 🐒 了 ^_^.

    Write in the first[写在最前] 对于从事 iOS 开发人员来说,当提到 ** runtime时,我想都可以说出来 「runtime 运行时」和基本使用的方法.相信很多开发者跟我当初一 ...

  6. iOS 模式详解—「runtime面试、工作」看我就 🐒 了 ^_^.

    引导 Copyright © PBwaterln Unauthorized shall not be *copy reprinted* . 对于从事 iOS 开发人员来说,所有的人都会答出「runti ...

  7. ios学习--详解IPhone动画效果类型及实现方法

    详解IPhone动画效果类型及实现方法是本文要介绍的内容,主要介绍了iphone中动画的实现方法,不多说,我们一起来看内容. 实现iphone漂亮的动画效果主要有两种方法,一种是UIView层面的,一 ...

  8. IOS中的手势详解

    1.点击 UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selecto ...

  9. iOS WKWebView详解

    UIWebView就不用说了,这个过时了,现在iOS8以后建议都使用WKWebView. WKWebView 是现代 WebKit API 在 iOS 8 和 OS X Yosemite 应用中的核心 ...

随机推荐

  1. WMI 硬件类简单介绍,默认打印机

    获取默认打印机: SELECT * FROM Win32_Printer WHERE Default=True 常用的几个WMI: 主板:Win32_BaseBoard http://msdn.mic ...

  2. 将BootstrapJS和AngularJS结合使用以及为什么不用jQuery

    原文网址链接:http://ourjs.com/detail/54e18c51232227083e000034Bootstrap  和 Angular  都是人们大量使用的工具.在很多项目中,它们需要 ...

  3. PHP循环语句基础介绍

    PHP 中的循环语句用于执行相同的代码块指定的次数. 循环 在您编写代码时,您经常需要让相同的代码块运行很多次.您可以在代码中使用循环语句来完成这个任务. 在 PHP 中,我们可以使用下列循环语句: ...

  4. 【SQL】sql版Split函数。用于拆分字符串为单列表格

    功能与.net版string.Split函数类似,只不过.net返回的是数组,这个返回的是一个单列表格,每个拆分出来的子串占一行.可选是否移除空格子串和重复项.市面上类似的函数不算少,但大多都是在循环 ...

  5. android 中layer-list的用法

    1.可以将多个图片按照顺序层叠起来 2.在drawable下建立一个xml文件 <layer-list xmlns:android="http://schemas.android.co ...

  6. 纯css的防止图片撑破页面的代码图片会自动按比例缩小

  7. 2015暑假多校联合---Zero Escape(变化的01背包)

    题目链接 http://acm.hust.edu.cn/vjudge/contest/130883#problem/C Problem Description Zero Escape, is a vi ...

  8. 泛函编程(26)-泛函数据类型-Monad-Applicative Functor Traversal

    前面我们讨论了Applicative.Applicative 就是某种Functor,因为我们可以用map2来实现map,所以Applicative可以map,就是Functor,叫做Applicat ...

  9. php中的字符串常用函数(五) explode 妙用

    // 示例 2 $data = "foo:*:1023:1000::/home/foo:/bin/sh" ; list( $user , $pass , $uid , $gid , ...

  10. 【FOL】第二周

    一直在忙其他事情,停了好久了.终于又可以开始做点东西了. 这周主要工作: 1.整理客户端代码,加入网络模块:fol.client.net. 2.写了个简单的版本服务端程序. 3.初步完成了自动更新功能 ...