也没有什么好说的,方法都差不多,只要记得当你想要同时实现两个或多个手势的话,要遵守<UIGestureRecognizerDelegate>协议,闲言休叙,直接上代码:

 #import "RootViewController.h"
#import "RootView.h" @interface RootViewController () <UIGestureRecognizerDelegate> // 手势识别器代理
@property (nonatomic, strong) RootView *rootView; @end @implementation RootViewController - (void)loadView {
self.rootView = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.view = self.rootView;
} - (void)viewDidLoad {
[super viewDidLoad]; // 轻拍
[self tapGesture]; // 平移
//[self panGesture]; // 缩放(捏合)
[self pinchGesture]; // 旋转
[self rotationGesture]; // 长按
[self longPressGesture]; // 轻扫(滑动)
[self swipeGesture]; // 屏幕边缘轻扫(滑动)
[self screenEdgePanGesture];
} #pragma mark - 轻拍手势 UITapGestureRecognizer
// 添加轻拍手势
- (void)tapGesture {
// 1.创建手势识别对象
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction:)]; // 2.将手势添加到相应视图上
[self.rootView.imageView addGestureRecognizer:tap];
} // 实现轻拍手势事件
- (void)tapGestureAction:(UITapGestureRecognizer *)tap {
NSLog(@"轻拍成功");
} #pragma mark - 平移手势 UIPanGestureRecognizer
// 添加平移手势
- (void)panGesture {
// 1.创建手势识别对象
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureAction:)]; // 2.将手势添加到相应的视图上
[self.rootView.imageView addGestureRecognizer:pan]; } // 实现平移手势事件
- (void)panGestureAction:(UIPanGestureRecognizer *)pan { NSLog(@"平移成功"); // 在view上面挪动的距离
CGPoint p = [pan translationInView:pan.view];
pan.view.transform = CGAffineTransformTranslate(pan.view.transform, p.x, p.y); // 清空移动的距离
[pan setTranslation:CGPointZero inView:pan.view];
} #pragma mark - 缩放手势(捏合)UIPinchGestureRecognizer
// 添加平移手势
- (void)pinchGesture {
// 1.创建手势识别对象
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGestureAction:)]; // 设置代理
pinch.delegate = self; // 2.将手势添加到相应的视图上
[self.rootView.imageView addGestureRecognizer:pinch]; } // 实现缩放手势事件
- (void)pinchGestureAction:(UIPinchGestureRecognizer *)pinch { pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
pinch.scale = ; } #pragma mark - 旋转手势 UIRotationGestureRecognizer
// 添加旋转手势
- (void)rotationGesture {
// 1.创建手势识别对象
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGestureAction:)]; // 设置代理
rotation.delegate = self; // 2.将手势添加到相应的视图上
[self.rootView.imageView addGestureRecognizer:rotation]; } // 实现旋转手势事件
- (void)rotationGestureAction:(UIRotationGestureRecognizer *)rotation { rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
rotation.rotation = ;
} #pragma mark - 长按手势 UILongPressGestureRecognizer
// 添加长按手势
- (void)longPressGesture {
// 1.创建手势识别对象
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureAction:)]; // 2.将手势添加到相应的视图上
[self.rootView.imageView addGestureRecognizer:longPress];
} // 实现长按手势事件
- (void)longPressGestureAction:(UILongPressGestureRecognizer *)longPress {
// 开始长按的时候执行
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"长按成功");
}
} #pragma mark - 轻扫手势 UISwipeGestureRecognizer
// 添加轻扫手势
- (void)swipeGesture {
// 向上滑动
// 1.创建手势识别对象
UISwipeGestureRecognizer *upSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureAction:)];
upSwipe.direction = UISwipeGestureRecognizerDirectionUp;
// 2.将手势添加到相应的视图上
[self.rootView.imageView addGestureRecognizer:upSwipe]; // 向下滑动
// 1.创建手势识别对象
UISwipeGestureRecognizer *downSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureAction:)];
downSwipe.direction = UISwipeGestureRecognizerDirectionDown;
// 2.将手势添加到相应的视图上
[self.rootView.imageView addGestureRecognizer:downSwipe]; // 向右滑动
// 1.创建手势识别对象
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureAction:)];
rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
// 2.将手势添加到相应的视图上
[self.rootView.imageView addGestureRecognizer:rightSwipe]; // 向左滑动
// 1.创建手势识别对象
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureAction:)];
leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
// 2.将手势添加到相应的视图上
[self.rootView.imageView addGestureRecognizer:leftSwipe]; } // 实现轻扫手势事件
- (void)swipeGestureAction:(UISwipeGestureRecognizer *)swipe {
if (swipe.direction == UISwipeGestureRecognizerDirectionUp) {
NSLog(@"向上滑动");
CGPoint p = CGPointMake(self.rootView.imageView.frame.origin.x, self.rootView.imageView.frame.origin.y - );
self.rootView.imageView.frame = CGRectMake(p.x, p.y, self.rootView.imageView.frame.size.width, self.rootView.imageView.frame.size.height);
} if (swipe.direction == UISwipeGestureRecognizerDirectionDown) {
NSLog(@"向下滑动");
CGPoint p = CGPointMake(self.rootView.imageView.frame.origin.x, self.rootView.imageView.frame.origin.y + );
self.rootView.imageView.frame = CGRectMake(p.x, p.y, self.rootView.imageView.frame.size.width, self.rootView.imageView.frame.size.height);
} if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"向左滑动");
CGPoint p = CGPointMake(self.rootView.imageView.frame.origin.x - , self.rootView.imageView.frame.origin.y);
self.rootView.imageView.frame = CGRectMake(p.x, p.y, self.rootView.imageView.frame.size.width, self.rootView.imageView.frame.size.height);
} if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(@"向右滑动");
CGPoint p = CGPointMake(self.rootView.imageView.frame.origin.x + , self.rootView.imageView.frame.origin.y);
self.rootView.imageView.frame = CGRectMake(p.x, p.y, self.rootView.imageView.frame.size.width, self.rootView.imageView.frame.size.height);
}
} #pragma mark - 屏幕边缘轻扫 UIScreenEdgePanGestureRecognizer
// 添加屏幕边缘轻扫手势(也有多个方式,我们这里只介绍一下,从屏幕顶部和左边轻扫,其他的都一样)
- (void)screenEdgePanGesture {
// 从屏幕顶部轻扫屏幕边缘
// 1.创建手势识别对象
UIScreenEdgePanGestureRecognizer *TopScreenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePanGestureAction:)];
TopScreenEdgePan.edges = UIRectEdgeTop;
// 2.将手势添加到相应的视图上
[self.rootView addGestureRecognizer:TopScreenEdgePan]; // 从屏幕左边轻扫屏幕边缘
// 1.创建手势识别对象
UIScreenEdgePanGestureRecognizer *leftScreenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePanGestureAction:)];
leftScreenEdgePan.edges = UIRectEdgeLeft;
// 2.将手势添加到相应的视图上
[self.rootView addGestureRecognizer:leftScreenEdgePan]; } // 实现屏幕边缘轻扫手势
- (void)screenEdgePanGestureAction:(UIScreenEdgePanGestureRecognizer *)screenEdgePan {
if (screenEdgePan.edges == UIRectEdgeTop) {
NSLog(@"从屏幕顶部轻扫屏幕边缘");
} if (screenEdgePan.edges == UIRectEdgeLeft) {
NSLog(@"从屏幕左边轻扫屏幕边缘");
}
} #pragma mark - 实现协议方法,同时识别多个手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end

iOS七大手势识别的更多相关文章

  1. IOS添加手势识别

    ios里面有手势识别,多点触控等功能,过去要实现手势识别很复杂,现在苹果为我们实现了,手势识别变得很简单 1.向视图添加手势识别器:(一般由controller完成,有时View也可以添加) 2.提供 ...

  2. iOS 七大手势之轻拍,长按,旋转手势识别器方法

    一.监听触摸事件的做法   如果想监听一个view上面的触摸事件,之前的做法通常是:先自定义一个view,然后再实现view的touches方法,在方法内部实现具体处理代码 通过touches方法监听 ...

  3. iOS 解析手势识别(Gesture Recognizers)

    一.Gesture Recognizers Gesture Recognizers是在iOS3.2引入的,可以用来识别手势.简化定制视图事件处理的对象.Gesture Recognizers的基类为U ...

  4. 关于ios的手势识别(GestureRecognizers)讲解

    大家都知道,苹果的设备,不管是mac机器还是iPhone或iad,都支持多点触控,进而延伸了多种手势识别的功能.这为用户带来了很大的便携性和多样灵活性,极大的方便了用户的使用.足以见手势识别(Gest ...

  5. iOS 七大手势之轻拍,长按,旋转手势识别器方法-赵小波

    一.监听触摸事件的做法 如果想监听一个view上面的触摸事件,之前的做法通常是:先自定义一个view,然后再实现view的touches方法,在方法内部实现具体处理代码 通过touches方法监听vi ...

  6. iOS基础 - 手势识别 与 手势说明

    一.使用手势识别的四个步骤 1> 实例化手势识别 - (id)initWithTarget:(id)target action:(SEL)action; 2> 设置手势识别属性 3> ...

  7. iOS图形手势识别框架SGGestureRecognizer

    简介 苹果官方为我们提供了简单手势的识别器,但对于图形手势,例如五角星.三角形等的识别,就需要自己实现了.通过识别这些手势,可以去执行特定的操作,或是输入公式.释放魔法等,可以为App增光添彩. 下载 ...

  8. iOS七大手势之(平移、捏合、轻扫、屏幕边缘轻扫)手势识别器方法

    使用手势很简单,分为两步: 创建手势实例.当创建手势时,指定一个回调方法,当手势开始,改变.或结束时,回调方法被调用. 添加到需要识别的View中.每个手势只对应一个View,当屏幕触摸在View的边 ...

  9. ios手势识别代理

    之前做优质派时写了个仿网易新闻导航的第三方,由于当时做项目时这个主控制器就是RootViewController,虽然用的是ScrollView但也没考虑到导航栏的手势返回的问题 ,现在做小区宝3.0 ...

随机推荐

  1. 数据可视化(4)--jqplot

    本来打算继续研究Google Charts,但上头下了指示让看jqplot,无奈,只好先将Google Charts放一放,不过真心觉得Google Charts不错,现在先开始jqplot. jqP ...

  2. 转载--CentOS 6.3下部署LVS(NAT)+keepalived实现高性能高可用负载均衡

    源地址:http://www.cnblogs.com/mchina/archive/2012/08/27/2644391.html 一.简介 VS/NAT原理图: 二.系统环境 实验拓扑: 系统平台: ...

  3. Java魔法堂:注解用法详解——@Override

    一.前言 现在有Son和Parent两个类,且类型Son将会重写类型Parent的getName函数.但不幸的是由于码农大意,写成如下代码: public class Parent{ public S ...

  4. JS魔法堂:关于元素位置和鼠标位置的属性

    一.关于鼠标位置的属性   1. 触发鼠标事件的区域 盒子模型中的border,padding,content区域会触发鼠标事件,点击margin区域将不触发鼠标事件.   2. 鼠标事件对象Mous ...

  5. JS魔法堂:IE5~9的Drag&Drop API

    一.前言     < HTML5魔法堂:全面理解Drag & Drop API>中提到从IE5开始已经支持DnD API,但IE5~9与HTML5的API有所不同,下面我们来了解一 ...

  6. [C#] CSharp 基本语法

    CSharp Language Specification 一.基础 1.规范: 除常量外,所有变量用驼峰命名方式,其它用帕斯卡命名方式. 2.编译: 首先由csc.exe将cs文件编译成MSIL.当 ...

  7. Mybatis 示例之 SelectKey

    SelectKey在Mybatis中是为了解决Insert数据时不支持主键自动生成的问题,他可以很随意的设置生成主键的方式. 不管SelectKey有多好,尽量不要遇到这种情况吧,毕竟很麻烦. sel ...

  8. Entity FrameWork 增删查改

    Add #region 1.0 新增+void Add() /// <summary> /// 新增 /// </summary> static void Add() { // ...

  9. OracleHelper(对增删改查分页查询操作进行了面向对象的封装,对批量增删改操作的事务封装)

    公司的一个新项目使用ASP.NET MVC开发,经理让我写个OracleHelper,我从网上找了一个比较全的OracleHelper类,缺点是查询的时候返回DataSet,数据增删改要写很多代码(当 ...

  10. SQL Server密码管理的六个危险判断

    当管理SQL Server内在的帐户和密码时,我们很容易认为这一切都相当的安全.但实际上并非如此.在这里,我们列出了一些对于SQL Server密码来说非常危险的判断. 当管理SQL Server内在 ...