UIGestureRecognizer(手势识别)在iOS 中非常重要,他极大地提高了移动设备的使用便捷性;

在3.2之前是主要使用的是由UIResponder而来的如下4种方式:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

但是由于这种方式需要自己计算做不同的手势分辨,实在麻烦。

在3.2 以后,提供了一些常用的手势(UIGestureRecognizer 的子类),开发者可以直接使用他们进行手势操作。

1、拍击——UITapGestureRecognizer

2、向里或向外捏——UIPinchGestureRecognizer

3、摇动或者拖拽——UIPanGestureRecognizer

4、擦碰——UISwipeGestureRecognizer

5、旋转——UIRotationGestureRecognizer 

6、长按——UILongPressGestureRecognizer 

每个手势识别器的用法都差不多,以UITapGestureRecognizer为例:

     //创建手势识别器对象并监听手势的触发
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
//连续敲击次数
tap.numberOfTapsRequired = ;
//需要两根手指一起敲击
tap.numberOfTouchesRequired = ;
//添加手势到指定的view上
[self.view addGestureRecognizer:tap];

敲击实例:

1、UITapGestureRecognizer 拍击

 //创建手势识别器对象并监听手势的触发
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
//连续敲击次数
tap.numberOfTapsRequired = ;
//需要两根手指一起敲击
tap.numberOfTouchesRequired = ;
//添加手势到指定的view上
[self.view addGestureRecognizer:tap]; - (void)tapAction:(UITapGestureRecognizer*)tapGesture{ NSLog(@"我敲击了屏幕"); CGPoint point = [tapGesture locationInView:self.view];
NSLog(@"tapAction:%f,%f",point.x,point.y);
}

输出:

2、UIPinchGestureRecognizer  向里或向外捏

     UIPinchGestureRecognizer *Pinch =[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(PinchAction:)];
Pinch.delegate = self;
[imageView addGestureRecognizer:Pinch]; - (void)PinchAction:(UIPinchGestureRecognizer*)pinchGesture{
pinchGesture.view.transform = CGAffineTransformScale(pinchGesture.view.transform, pinchGesture.scale, pinchGesture.scale);
pinchGesture.scale = ;
}

3、UIPanGestureRecognizer  摇动或者拖拽


//先放置一张图片实施拖拽

imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"u=3971024035,4095552302&fm=21&gp=0"]];

    [imageView setFrame:(CGRect){50,50,100,100}];

    [self.view addSubview:imageView];


    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
pan.delegate = self;
[self.view addGestureRecognizer:pan]; //设置触发拖拽的最少触摸点,默认为1
pan.minimumNumberOfTouches = ;
//设置触发拖拽的最多触摸点
pan.maximumNumberOfTouches = ; - (void)panAction:(UIPanGestureRecognizer *)panGesture{
CGPoint point = [panGesture locationInView:self.view]; NSLog(@"panAction:(%f, %f)",point.x, point.y);
imageView.center = point; if (panGesture.state==UIGestureRecognizerStateBegan) {
NSLog(@"panAction StateBegan:(%f,%f)", point.x, point.y);
} if (panGesture.state==UIGestureRecognizerStateChanged) {
NSLog(@"panAction StateChanged:(%f,%f)", point.x, point.y);
} if (panGesture.state==UIGestureRecognizerStateEnded) {
NSLog(@"panAction StateEnded:(%f,%f)", point.x, point.y);
} }

输出:

4、UISwipeGestureRecognizer  擦碰

     UISwipeGestureRecognizer *leftSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeAction:)];
[self.view addGestureRecognizer:leftSwipeGesture];
leftSwipeGesture.direction = UISwipeGestureRecognizerDirectionLeft; UISwipeGestureRecognizer *rightSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeAction:)];
[self.view addGestureRecognizer:rightSwipeGesture];
rightSwipeGesture.direction = UISwipeGestureRecognizerDirectionRight; - (void)SwipeAction:(UISwipeGestureRecognizer*)swipeGesture{
if (swipeGesture.direction==UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"swipeGesture:Left");
}
if (swipeGesture.direction==UISwipeGestureRecognizerDirectionRight) {
NSLog(@"swipeGesture:Right");
}
}

输出:

5、UIRotationGestureRecognizer  旋转

 UIRotationGestureRecognizer *rotations = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
rotations.delegate = self;
rotation = ;
[self.view addGestureRecognizer:rotations]; - (void)rotationAction:(UIRotationGestureRecognizer*)rotationGesture{
imageView.transform = CGAffineTransformMakeRotation(rotation+rotationGesture.rotation); if (rotationGesture.state==UIGestureRecognizerStateEnded) {
rotation += rotationGesture.rotation;
} }

6、UILongPressGestureRecognizer  长按

     UILongPressGestureRecognizer *longp = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longAction:)];
[self.view addGestureRecognizer:longp]; longp.numberOfTouchesRequired = ;
//长按时间为2秒
longp.minimumPressDuration = ;
//允许15秒中运动
longp.allowableMovement = ; - (void)longAction:(UILongPressGestureRecognizer *)longPress{
CGPoint point = [longPress locationInView:self.view];
imageView.center = point; if(longPress.state == UIGestureRecognizerStateBegan){
NSLog(@"longAction StateBegan: (%f, %f)", point.x, point.y);
} if(longPress.state == UIGestureRecognizerStateEnded){
NSLog(@"longAction StateEnded: (%f, %f)", point.x, point.y);
}
}

输出:

iOS基础篇(十七)——UIGestureRecognizer用法的更多相关文章

  1. ios基础篇(十八)——Delegate 、NSNotification 和 KVO用法及其区别

    一.Delegate Delegate本质是一种程序设计模型,iOS中使用Delegate主要用于两个页面之间的数据传递.iphone中常用@protocol和delegate的机制来实现接口的功能. ...

  2. ios基础篇(十四)——UITableView(二)属性及基本用法

    上一篇说了UITableView的重用机制,让我们对UITableView有了简单了解,下面说说UITableView的属性及常见方法. 一.属性 1.frame:设置控件的尺寸和大小 2.backg ...

  3. ios基础篇(二十七)—— Json解析

    一.什么是Json JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使 ...

  4. iOS基础篇(十五)——UIScrollView的基本用法

    滚动视图(UIScrollView)通常用于显示内容尺寸大于屏幕尺寸的视图. 一.基本属性 1.CGSize contentSize :设置UIScrollView的滚动范围 2.CGPoint co ...

  5. ios基础篇(七)——UISwich、UISlider、UIProgressView的用法总结

    一.UISlider UIslider滑块控件在IOS开发中会常用到,可用于调节音量,字体大小等UI方面的交互:UISlider实例提供一个控件,让用户通过左右拖动一个滑块(可称其为“缩略图”)来选择 ...

  6. ios基础篇(二)——UIImageView的常见用法

    UIImageView是在界面上显示图片的一个控件,在UIImageView中显示图片的话应该首先把图片加载到UIImage中,然后通过其他方式使用该UIImage. 创建UIImageView有两种 ...

  7. ios基础篇(一)——UIView控件基本属性与常见用法

    1.frame 控件的位置和尺寸(以父控件的左上角为坐标原点(0,0)) 2.center 控件的中点(以父控件的左上角为坐标原点) 3.bounds 控件的位置和尺寸(以自己的左上角为坐标原点(0, ...

  8. ios基础篇(十六)——UIWebView的基本使用

    UIWebView是内置的浏览器控件,可以用它来浏览网页.打开文档等.UIWebView是一个混合体,具体的功能控件内置的,实现一些基本的功能.UIWebView可以查看Html网页,pdf文件,do ...

  9. ios基础篇(二十九)—— 多线程(Thread、Cocoa operations和GCD)

    一.进程与线程 1.进程 进程是指在系统中正在运行的一个应用程序,每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内: 如果我们把CPU比作一个工厂,那么进程就好比工厂的车间,一个工厂有 ...

随机推荐

  1. 微软良心之作——Visual Studio Code 开源免费跨平台代码编辑器

    微软良心之作——Visual Studio Code 开源免费跨平台代码编辑器 在 Build 2015 大会上,微软除了发布了 Microsoft Edge 浏览器和新的 Windows 10 预览 ...

  2. RDIFramework.NET ━ 9.5 组织机构管理 ━ Web部分

    RDIFramework.NET ━ .NET快速信息化系统开发框架 9.5 组织机构管理 -Web部分 组织机构管理模块提供直观方便的组织机构管理,以树型结构显示单位和部门的机构体系,可根据需要进行 ...

  3. UE4 减少APK包的大小

    本文依据官方文档 Reducing APK Package Size整理而来,不过我会陆续添加自己减少包大小的心得. ETC1 纹理 当使用ETC1打Android包时,注意ETC1是不会压缩带Alp ...

  4. pb数据窗口设置操作

    1 使DataWindow列只能追加不能修改如何使DataWindow中的数据只能追加新记录而不能修改,利用 Column 的 Protect 属性可以很方便的做到这一点,方法如下:将每一列的 Pro ...

  5. 十三、Java基础---------多线程总结

    多线程概述 理解多线程首先应明确线程,要了解线程就必须了解什么是进程. 1.进程 是一个正在执行的程序. 每一个进程执行都有一个执行顺序.该顺序是一个执行路径,或者叫一个控制单元. 2.线程 就是进程 ...

  6. android stutio 快捷键

    [F] [F] F2 在错误代码之间切换 F3 往前定位(Shift + F3:往后定位 )有问题 F4\Ctrl+鼠标点击\Ctrl+B 转到定义,查看类继承关系 F5 但不调试进入函数内部. F6 ...

  7. checkbox样式自定义

    1.使用两张图片(选中和未选中),创建一个选择器. 2.使用checkbox的   drawableLeft  drawableRight  等几个属性把选择器设置进去 3.checkbox的butt ...

  8. Android 进阶Android 中的 IOC 框架 【ViewInject】 (上)

    1.概述 首先我们来吹吹牛,什么叫IoC,控制反转(Inversion of Control,英文缩写为IoC),什么意思呢? 就是你一个类里面需要用到很多个成员变量,传统的写法,你要用这些成员变量, ...

  9. 我的android学习经历34

    用类对象作为ArrayAdapter绑定的基本数据类型(和SimpleAdater效果类似) 一般ArrayAdapter绑定的基本数据类型是String,接下来介绍一下类对象作为基本数据类型: 首先 ...

  10. 【Unity3D游戏开发】之常用代码 (十二)

    //创建一个名为"Player"的游戏物体 //并给他添加刚体和立方体碰撞器. player=new GameObject("Player"); player. ...