手势识别在iOS上非常重要,手势操作移动设备的重要特征,极大的增加了移动设备使用便捷性。

iOS系统在3.2以后,为方便开发这使用一些常用的手势,提供了UIGestureRecognizer类。手势识别UIGestureRecognizer类是个抽象类,下面的子类是具体的手势,开发这可以直接使用这些手势识别。

UITapGestureRecognizer

UIPinchGestureRecognizer

UIRotationGestureRecognizer

UISwipeGestureRecognizer

UIPanGestureRecognizer

UILongPressGestureRecognizer

    • 上面的手势对应的操作是:
  • Tap(点一下)
  • Pinch(二指往內或往外拨动,平时经常用到的缩放)
  • Rotation(旋转)
  • Swipe(滑动,快速移动)
  • Pan (拖移,慢速移动)
  • LongPress(长按)

2、使用手势的步骤

使用手势很简单,分为两步:

  • 创建手势实例。当创建手势时,指定一个回调方法,当手势开始,改变、或结束时,回调方法被调用。
  • 添加到需要识别的View中。每个手势只对应一个View,当屏幕触摸在View的边界内时,如果手势和预定的一样,那就会回调方法。

ps:一个手势只能对应一个View,但是一个View可以有多个手势。

手势具体操作 如下:

新建一个mainViewController 继承于 UIViewcontroller ,

mainViewController.m

#import "mainViewController.h"

@interface mainViewController ()

@property (retain, nonatomic)UIImageView *imageView;

@end

@implementation mainViewController

- (void)dealloc{

[self.imageView release];

[super dealloc];

}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

if (self) {

}

return self;

}

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

[self createSubViews];

}

- (void)createSubViews{

self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(70, 130, 220, 350)];

[_imageView setImage:[UIImage imageNamed:@"1.jpg"]];

_imageView.userInteractionEnabled = YES;

[self.view addSubview:_imageView];

[_imageView release];

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"点击", @"长按", @"轻扫", @"捏合", @"旋转", @"拖拽", nil]];

segmentedControl.frame = CGRectMake(30, 600, 315, 50);

segmentedControl.momentary = YES;

segmentedControl.tintColor = [UIColor blackColor];

[segmentedControl addTarget:self action:@selector(action:) forControlEvents:UIControlEventValueChanged];

[self.view addSubview:segmentedControl];

[segmentedControl release];

}

#pragma mari - UISegmentedControl的点击事件

- (void)action:(id)sender{

// 分段点击按钮

UISegmentedControl *seg = (UISegmentedControl *)sender;

// 移除imageView的所有手势,重新添加新的手势

// 第一步:获得一个视图的所有手势

NSArray *gestures = self.imageView.gestureRecognizers;

// 第二步:移除

for (UIGestureRecognizer *ges in gestures) {

[self.imageView removeGestureRecognizer:ges];

}

switch (seg.selectedSegmentIndex) {

case 0:

{

// 创建一个点击手势

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];

[self.imageView addGestureRecognizer:tap];

[tap release];

}

break;

case 1:

{

// 长按手势

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];

[self.imageView addGestureRecognizer:longPress];

[longPress release];

}

break;

case 2:

{

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];

// 一个轻扫手势只能有一个轻扫方向

// 设置轻扫的方向

swipe.direction = UISwipeGestureRecognizerDirectionDown;

[self.imageView addGestureRecognizer:swipe];

[swipe release];

}

break;

case 3:

{

// 捏合手势

UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAciton:)];

[self.imageView addGestureRecognizer:pinch];

[pinch release];

}

break;

case 4:

{

// 旋转手势

UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateAction:)];

[self.imageView addGestureRecognizer:rotate];

[rotate release];

}

break;

case 5:

{

// 拖拽手势

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];

[self.imageView addGestureRecognizer:pan];

[pan release];

}

break;

default:

break;

}

}

#pragma mark - 内部的点击手势事件

#pragma mark 点击

- (void)tapAction:(UITapGestureRecognizer *)tap{

// 通过手势获得手势所在的视图

//    UIImageView *aImage = (UIImageView *)tap.view;

// 通过不同的状态做不同的事

if (tap.state == UIGestureRecognizerStateBegan) {

NSLog(@"开始点击");

}

NSLog(@"%s", __func__);

}

#pragma mark 长按

- (void)longPressAction:(UILongPressGestureRecognizer *)press{

if (press.state == UIGestureRecognizerStateBegan) {

NSLog(@"begin");

}

}

#pragma mark 轻扫

- (void)swipeAction:(UISwipeGestureRecognizer *)swipe{

NSLog(@"横扫千军");

}

#pragma mark 捏合

- (void)pinchAciton:(UIPinchGestureRecognizer *)pinch{

NSLog(@"捏合");

self.imageView.transform = CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);

pinch.scale = 1.0f;

}

#pragma mark 旋转

- (void)rotateAction:(UIRotationGestureRecognizer *)ges{

// 旋转的基础角度为0, 每次旋转的角度都是以当前中轴线为轴来判定的

self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, ges.rotation);

ges.rotation = 0;

NSLog(@"Rotation = %f", ges.rotation);

}

#pragma mark 拖拽

- (void)panAction:(UIPanGestureRecognizer *)pan{

CGPoint point = [pan translationInView:self.imageView];

// 偏移量会在每次偏移之后,在偏移后的基础上增加

self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, point.x, point.y);

// 重新把偏移量归零

[pan setTranslation:CGPointZero inView:self.imageView];

}

AppDelegate.m

#import "AppDelegate.h"

#import "mainViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (void)dealloc{

[super dealloc];

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after application launch.

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

_window.backgroundColor = [UIColor whiteColor];

[_window makeKeyAndVisible];

[_window release];

mainViewController *myView = [[mainViewController alloc] init];

[self.window setRootViewController:myView];

[myView release];

return YES;

}

iOS 手势的使用 六个手势 轻拍 长按 拖拽 捏合 轻扫 旋转相关推荐

  1. Android开发学习之ImageView手势拖拽、缩放、旋转

    在Android应用中,图片随手势的拖拽.缩放.旋转在很多场景中都会用到,今天我们要做的就是在ImageView的基础上实现一个可以拖拽.缩放.转转的TouchView. 一.实现原理 OnTouch ...

  2. 小程序的拖拽、缩放和旋转手势

    在开发中,有时会遇到像App中的手势那样的效果,下面就仿照App实现了一下. wxml部分: <view class="touch-container"><vie ...

  3. iOS开发——仿微信图片浏览交互的实现(向下拖拽图片退出图片浏览器)

    点击上方"iOS开发",选择"置顶公众号" 关键时刻,第一时间送达! DEMO的github地址:https://github.com/YYProgrammer ...

  4. 微信小程序的拖拽、缩放和旋转手势

    在开发中,有时会遇到像App中的手势那样的效果,下面就仿照App实现了一下. wxml部分: <view class="touch-container"><vie ...

  5. Swift开发:仿Clear手势操作(拖拽、划动、捏合)UITableView

    2019独角兽企业重金招聘Python工程师标准>>> 这是一个完全依靠手势的操作ToDoList的演示,功能上左划删除,右划完成任务,拖拽调整顺序,捏合张开插入. 项目源码: ht ...

  6. iOS手势全埋点:轻拍手势、长按手势、捏合手势、旋转手势、轻扫手势、平移手势、屏幕边缘平移手势

    文章目录 前言 I.手势全埋点方案 1.1 轻拍手势全埋点 1.2 长按手势全埋点 II .右划返回的事件与scrollView滚动事件冲突的解决方案 see also 前言 由于UIGestureR ...

  7. iOS事件机制,以及不同手势使用touchesBegan等表现

    2019独角兽企业重金招聘Python工程师标准>>> //事件处理方法 UIResponder中定义了一系列对事件的处理方法,他们分别是: –(void)touchesBegan: ...

  8. iOS 全屏侧滑手势/UIScrollView/UISlider间滑动手势冲突

    七夕节福利 效果预览 一.前期准备 有一个支持全屏侧滑返回的视图控制器ViewController,ViewController.view上有一个UIScrollView,UIScrollView上有 ...

  9. uni-app、H5+ 仿IOS 实现 安卓手势拖拽右滑关闭当前页面并返回上级页面 + 阴影效果(侧滑返回)

    背景 在我们日常开发中,经常会效仿某些流行APP渲染动画效果,用来满足自己开发或公司开发的日常需要,学习一下新的东西还是好的,本案例是 仿IOS 手势拖拽右滑 关闭当前页面并返回主页的一个案例 预览效 ...

最新文章

  1. Q学习(Q learning) 强化学习
  2. [?]Oracle 10g sqlplus 的Bug?
  3. Scilab 求解线性方程组示例(linsolve)
  4. @Autowired 与@Resource的区别
  5. 浅析GitLab Flow的十一个规则
  6. PIO导出Excel 设置样式
  7. 如何定时备份远程mysql数据库
  8. 药品研发的项目化管理
  9. ABYY FineReader PDF软件最新15个人​版安装下载步骤教程
  10. Mac终端常用命令及报错处理
  11. 开源SWD脱机烧录器-第一章 软硬件配置及其初始化
  12. OTB和VOT的评估指标
  13. 2021百度Create大会(暨百度AI开发者大会)
  14. 零基础编写图片服务器(1)
  15. PyTorch的生态和模型部署
  16. 尼尔森报告指出,宅经济成快速消费品在农历新年的商机
  17. 悟空CRM系统项目实测
  18. iPad 2下月登陆香港 水货应声降价
  19. 玉米社:巧用以下6点,规避短视频剪辑违规侵权问题
  20. CAD图纸如何免费导出成Word文档?

热门文章

  1. 关于安卓启动模拟器时出现~~~~have you declared this activity in your AndroidMainfest.xml?问题
  2. python 爱心代码
  3. 自己试用期还没过就开始带实习生是什么鬼?
  4. 基于bootdo制作一个restful模块
  5. 互联网大厂考点(阿里+百度+腾讯+字节跳动+美团+京东)
  6. 案例3-1-1 构建旁挂二层组网隧道转发WLAN
  7. 2.Latex安装和TeXworks Editor基础
  8. 相分离——下一代表观遗传修饰?
  9. 在Power BI中用DAX计算现值PV
  10. orbslam2稠密地图转octomap