也没有什么好说的,方法都差不多,只要记得当你想要同时实现两个或多个手势的话,要遵守<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. Struts2知多少(1) Struts2 MVC架构

    模型视图控制器(Model View Controller)或MVC,MVC是俗称,是一种软件设计模式,用于开发Web应用程序.模型 - 视图 - 控制器模式是由以下三个部分组成: Model - 模 ...

  2. jquery getJSON

    function onNodeClick(data) {            //只能选择体检分组            if (data.GroupType == 1) {             ...

  3. 关于鼠标事件的screenY,pageY,clientY,layerY,offsetY属性 (详细图解)

    screenY 鼠标相对于显示器屏幕左上角的偏移 pageY 鼠标相对于页面左上角的偏移 (其值不会受滚动条的影响) IE9之下并不支持这个属性 但是可以写点代码计算出来. jQuery中的实现: / ...

  4. AEM - Adobe CMS 扒坑记之始

    AEM是Adobe公司所出的商业内容管理系统,全称阿豆比体验管理系统(Adobe Experience Manager),其前身叫CQ,分别有CQ5 CQ6两个大版本.它提供了整套的网站内容管理系统解 ...

  5. 2016 SDCC会后总结

    很荣幸作为前端专题讲师参加2016年SDCC,与周爱民老师同台,听业界牛人的分享真是受益匪浅.对我来说是第一次在如此规模的专题论坛上演讲,全程紧张的要命,提前准备好的内容有很多因为紧张没有讲出来.此次 ...

  6. Linux常用命令回顾

    文件操作:ls 查看文件ls -a 查看全部文件,包括隐藏文件(以.开头)ls -l 查看文件的详细信息(ll同样的效果)ls -lh 查看文件的详细信息,人性化显示,文件大小标注单位文件权限-代表文 ...

  7. 我理解的Android加载器

    Android的加载器(loader)是从Android 3.0开始出来的东西.要理解这里需要先理解为什么会出现加载器(也有地方把它说成是装载器)呢? 如果没有加载器... 首先Activity是我们 ...

  8. asp.net MVC ajax上传文件

    普通上传 view: <body> <form id="form1" method="post" action="@Url.Acti ...

  9. SQLServer根据不同前缀生成多套流水号

    --种子表 --@prefix 前缀 --@seed 种子值 create table RefNoSeed( prefix ) unique, seed int ) go --测试表 --@inser ...

  10. 泛函编程(8)-数据结构-Tree

    上节介绍了泛函数据结构List及相关的泛函编程函数设计使用,还附带了少许多态类型(Polymorphic Type)及变形(Type Variance)的介绍.有关Polymorphism的详细介绍会 ...