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

 更新时间:2017年03月27日 15:43:51   作者:这酸爽!  
IOS中手势操作一般是 UIGestureRecognizer 类的几个手势子类去实现,一般我们用到的手势就这么5种,具体哪几种大家通过本文学习吧,本文重点给大家介绍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中各种手势操作实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

  • 超全的iOS各种设备信息获取方法总结(包括iPhone8/iPhone X)

    超全的iOS各种设备信息获取方法总结(包括iPhone8/iPhone X)

    这篇文章主要给大家介绍了关于iOS各种设备信息获取方法,iPhone8/iPhone X的后驱详细信息也已更新,文中给出了详细的示例代码供大家参考学习,需要的朋友们下面随着小编来一起学习学习吧。
    2017-12-12
  • iOS算法教程之分段截取常数示例

    iOS算法教程之分段截取常数示例

    这篇文章主要给大家介绍了关于iOS算法教程之分段截取常数的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
    2018-01-01
  • iOS自定义UICollectionViewFlowLayout实现图片浏览效果

    iOS自定义UICollectionViewFlowLayout实现图片浏览效果

    这篇文章主要介绍了iOS自定义UICollectionViewFlowLayout实现图片浏览效果的相关资料,需要的朋友可以参考下
    2016-03-03
  • iOS 对当前webView进行截屏的方法

    iOS 对当前webView进行截屏的方法

    下面小编就为大家带来一篇iOS 对当前webView进行截屏的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • ios使用NSProxy实现消息转发

    ios使用NSProxy实现消息转发

    本文主要介绍了ios使用NSProxy实现消息转发,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • iOS自定义可展示、交互的scrollView滚动条

    iOS自定义可展示、交互的scrollView滚动条

    这篇文章主要为大家详细介绍了iOS自定义可展示、交互的scrollView滚动条,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-04-04
  • 剖析iOS开发中Cocos2d-x的内存管理相关操作

    剖析iOS开发中Cocos2d-x的内存管理相关操作

    这篇文章主要介绍了剖析iOS开发中Cocos2d-x的内存管理相关操作,Cocos2d-x是开发游戏的利器,需要的朋友可以参考下
    2015-10-10
  • 浅谈iOS 屏幕方向那点事儿

    浅谈iOS 屏幕方向那点事儿

    这篇文章主要介绍了浅谈iOS 屏幕方向那点事儿,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • iOS开发之App主题切换解决方案完整版(Swift版)

    iOS开发之App主题切换解决方案完整版(Swift版)

    这篇文章主要为大家详细介绍了iOS开发之App主题切换完整解决方案,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-02-02
  • iOS实现PDF文件浏览功能

    iOS实现PDF文件浏览功能

    这篇文章主要为大家详细介绍了iOS实现PDF文件浏览功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-04-04

最新评论