iOS七大手势识别
也没有什么好说的,方法都差不多,只要记得当你想要同时实现两个或多个手势的话,要遵守<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七大手势识别的更多相关文章
- IOS添加手势识别
ios里面有手势识别,多点触控等功能,过去要实现手势识别很复杂,现在苹果为我们实现了,手势识别变得很简单 1.向视图添加手势识别器:(一般由controller完成,有时View也可以添加) 2.提供 ...
- iOS 七大手势之轻拍,长按,旋转手势识别器方法
一.监听触摸事件的做法 如果想监听一个view上面的触摸事件,之前的做法通常是:先自定义一个view,然后再实现view的touches方法,在方法内部实现具体处理代码 通过touches方法监听 ...
- iOS 解析手势识别(Gesture Recognizers)
一.Gesture Recognizers Gesture Recognizers是在iOS3.2引入的,可以用来识别手势.简化定制视图事件处理的对象.Gesture Recognizers的基类为U ...
- 关于ios的手势识别(GestureRecognizers)讲解
大家都知道,苹果的设备,不管是mac机器还是iPhone或iad,都支持多点触控,进而延伸了多种手势识别的功能.这为用户带来了很大的便携性和多样灵活性,极大的方便了用户的使用.足以见手势识别(Gest ...
- iOS 七大手势之轻拍,长按,旋转手势识别器方法-赵小波
一.监听触摸事件的做法 如果想监听一个view上面的触摸事件,之前的做法通常是:先自定义一个view,然后再实现view的touches方法,在方法内部实现具体处理代码 通过touches方法监听vi ...
- iOS基础 - 手势识别 与 手势说明
一.使用手势识别的四个步骤 1> 实例化手势识别 - (id)initWithTarget:(id)target action:(SEL)action; 2> 设置手势识别属性 3> ...
- iOS图形手势识别框架SGGestureRecognizer
简介 苹果官方为我们提供了简单手势的识别器,但对于图形手势,例如五角星.三角形等的识别,就需要自己实现了.通过识别这些手势,可以去执行特定的操作,或是输入公式.释放魔法等,可以为App增光添彩. 下载 ...
- iOS七大手势之(平移、捏合、轻扫、屏幕边缘轻扫)手势识别器方法
使用手势很简单,分为两步: 创建手势实例.当创建手势时,指定一个回调方法,当手势开始,改变.或结束时,回调方法被调用. 添加到需要识别的View中.每个手势只对应一个View,当屏幕触摸在View的边 ...
- ios手势识别代理
之前做优质派时写了个仿网易新闻导航的第三方,由于当时做项目时这个主控制器就是RootViewController,虽然用的是ScrollView但也没考虑到导航栏的手势返回的问题 ,现在做小区宝3.0 ...
随机推荐
- C# 解析百度天气数据,Rss解析百度新闻以及根据IP获取所在城市
百度天气 接口地址:http://api.map.baidu.com/telematics/v3/weather?location=上海&output=json&ak=hXWAgbsC ...
- Gamma Gamma~!!!
左图是没有进行gamma矫正的,右图是进行了gamma矫正的.以前一直以为是Tone Map的公式计算有问题,后来看PBR的paper时候,终于明白了gamma的重要性,一改,果然发现颜色不想以前那么 ...
- ruby -- 进阶学习(三)Strong Parameters在rail3.0和4.0中的区别
今天coding的时候遇到一个未知的类型,于是用puts logo_params.class查了下数据类型,然后google了一下发现是 Strong Parameter Strong paramet ...
- Android自定义之仿360Root大师水纹效果
很简单的一个效果.我直接上图了. 代码就不详细说了,贴出关键代码就行了. @Override protected void onDraw(Canvas canvas) { s ...
- [git]安装git-pylint-commit-hook提高python项目中的代码质量
什么是'git-pylint-commit-hook' 我在工作中,团队为了保证代码和提高代码的质量,要求每个项目都要求安装git-pylint-commit-hook,它是个钩子,会在你提交代码到本 ...
- [linux笔记]理清linux安装程序用到的(configure, make, make install)
我作为一名经常和linux打交道的程序员,每次在linux安装软件都祈求可以用——apt-get,yum,brew等应用程序管理器安装,有的时候事与愿违,你只能自己编译安装-wtf,说好的美丽世界呢? ...
- webapp开发之需要知道的css细节
引言 首先说明一下,本文分享对象是涉世未深的移动开发者,至于有经验的开发者可以自行绕道. 说来惭愧,做了几年pc端的前端开发,很少接触到webapp的移动端开发.如今有机会参与webapp的开发,发现 ...
- JavaScript中的数组详解
JavaScript中的数组 一.数组的定义 数组是值的有序集合,或者说数组都是数据的有序列表. 二.创建数组 [字面量形式] 1.空数组 var arr=[]; 2.带有元素的数组 var arr= ...
- SQL语句技巧之去除重复行
去除表中重复行数据,可能大家立马就想到的是用DISINTCT关键字,但DISINTCT只能是去除表中所有列都相同的行,若碰到需要去除表中多个字段重复的行(即:部份相同,部份不相同),那么该如何做呢?我 ...
- iOS实现图像素描效果
使用GPUImageSketchFilter对象实现图像素描效果 NSString *const kGPUImageSketchFragmentShaderString = SHADER_STRING ...