也没有什么好说的,方法都差不多,只要记得当你想要同时实现两个或多个手势的话,要遵守<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. Android抽象布局——include、merge 、ViewStub

    http://blog.csdn.net/xyz_lmn/article/details/14524567

  2. [转载]我们可以用SharePoint做什么

    前言 不知不觉作为一个SharePoint的开发人员若干年了,从SharePoint API开始学习,到了解SharePoint的结构,逐渐一点点了解sharepoint的体系:从SharePoint ...

  3. 2 Servlet基础

    作者:禅楼望月(http://www.cnblogs.com/yaoyinglong) 1. 从浏览器访问Servlet的流程 刚才发现,这里的图片不能正常显示,所以我给个链接,大家可以下载下来看从浏 ...

  4. SQL Server中的事务日志管理(5/9):完整恢复模式里的日志管理

    当一切正常时,没有必要特别留意什么是事务日志,它是如何工作的.你只要确保每个数据库都有正确的备份.当出现问题时,事务日志的理解对于采取修正操作是重要的,尤其在需要紧急恢复数据库到指定点时.这系列文章会 ...

  5. [Solution] Microsoft Windows 服务(2) 使用Topshelf创建Windows服务

    除了通过.net提供的windows服务模板外,Topshelf是创建Windows服务的另一种方法. 官网教程:http://docs.topshelf-project.com/en/latest/ ...

  6. WampServer 给电脑搭建apache服务器和php环境

    WampServer 给电脑搭建apache服务器和php环境 前端不仅要做页面展示层,还负责着数据交互的部分,不要等到后端人员做好工作了前端才开始对接,那样太被动了. 前端在完成静态页面的编码后,就 ...

  7. 【WinRT】国内外 Windows 应用商店应用开发者博客收集

    本文格式:博主名 博客链接 本人点评.排名不分先后. 中文: 博客园: webabcd http://www.cnblogs.com/webabcd/ 微软最有价值专家(MVP),他做的 Win8.1 ...

  8. csharp: Domain-Driven Design(领域驱动设计)

    http://dddsample.sourceforge.net/ https://github.com/citerus/dddsample-core http://dddsamplenet.code ...

  9. U-boot的环境变量值得注意的有两个: bootcmd 和bootargs

    本文转载至:http://www.cnblogs.com/cornflower/archive/2010/03/27/1698279.html U-boot的环境变量值得注意的有两个: bootcmd ...

  10. fibonacci数列的和取余(2)

    Maybe ACMers of HIT are always fond of fibonacci numbers, because it is so beautiful. Don't you thin ...