iOS开发之手势识别汇总
iOS开发之手势识别汇总
iOS开发中手势识别有六种:
轻击手势(TapGestureRecognizer),
轻扫手势 (SwipeGestureRecognizer),
长按手势(LongPressGestureRecognizer),
拖动手势(PanGestureRecognizer),
捏合手势(PinchGestureRecognizer),
旋转手势(RotationGestureRecognizer),
1,轻击手势(TapGestureRecognizer)
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesture:)];
tapGesture.numberOfTapsRequired = 1; //点击次数
tapGesture.numberOfTouchesRequired = 1; //点击手指数
[self.view addGestureRecognizer:tapGesture];
//轻击手势触发方法
-(void)tapGesture:(UITapGestureRecognizer *)sender
{
//your code
}
2,长按手势(LongPressGestureRecognizer)
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
//设置长按时间
longPressGesture.minimumPressDuration = 0.5;
[self.view addGestureRecognizer:longPressGesture];
//长按手势触发方法
-(void)longPressGesture:(id)sender
{
UILongPressGestureRecognizer *longPress = sender;
if (longPress.state == UIGestureRecognizerStateBegan)
{
//your code
}
}
说明:长按手势的常用状态如下
开始:UIGestureRecognizerStateBegan
改变:UIGestureRecognizerStateChanged
结束:UIGestureRecognizerStateEnded
取消:UIGestureRecognizerStateCancelled
失败:UIGestureRecognizerStateFailed
3,轻扫手势(SwipeGestureRecognizer)
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
//设置轻扫的方向
swipeGesture.direction = UISwipeGestureRecognizerDirectionRight; //向右
[self.view addGestureRecognizer:swipeGesture];
UISwipeGestureRecognizer *swipeGestureLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
//设置轻扫的方向
swipeGestureLeft.direction = UISwipeGestureRecognizerDirectionLeft; //向左
[self.view addGestureRecognizer:swipeGestureLeft];
//轻扫手势触发方法
-(void)swipeGesture:(id)sender
{
UISwipeGestureRecognizer *swipe = sender;
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft)
{
//向左轻扫
}
if (swipe.direction == UISwipeGestureRecognizerDirectionRight)
{
//向右轻扫
}
}
4,捏合手势(PinchGestureRecognizer)
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGesture:)];
[self.view addGestureRecognizer:pinchGesture];
////捏合手势触发方法
-(void) pinchGesture:(id)sender
{
UIPinchGestureRecognizer *gesture = sender;
//手势改变时
if (gesture.state == UIGestureRecognizerStateChanged)
{
//捏合手势中scale属性记录的缩放比例
_imageView.transform = CGAffineTransformMakeScale(gesture.scale, gesture.scale);
}
//结束后恢复
if(gesture.state==UIGestureRecognizerStateEnded)
{
[UIView animateWithDuration:0.5 animations:^{
_imageView.transform = CGAffineTransformIdentity;//取消一切形变
}];
}
}
5,拖动手势(PanGestureRecognizer)
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)];
[self.view addGestureRecognizer:panGesture];
//拖动手势触发方法
-(void) panGesture:(id)sender
{
UIPanGestureRecognizer *panGesture = sender;
CGPoint movePoint = [panGesture translationInView:self.view];
//your code
}
6,旋转手势(RotationGestureRecognizer)
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGesture:)];
[self.view addGestureRecognizer:rotationGesture];
//旋转手势触发方法
-(void)rotationGesture:(id)sender
{
UIRotationGestureRecognizer *gesture = sender;
if (gesture.state==UIGestureRecognizerStateChanged)
{
_imageView.transform=CGAffineTransformMakeRotation(gesture.rotation);
}
if(gesture.state==UIGestureRecognizerStateEnded)
{
[UIView animateWithDuration:1 animations:^{
_imageView.transform=CGAffineTransformIdentity;//取消形变
}];
}
}
本文出处刚刚在线:http://www.superqq.com/blog/2015/01/14/ioskai-fa-zhi-shou-shi-shi-bie-hui-zong/
iOS开发之手势识别汇总的更多相关文章
- iOS开发之手势识别
感觉有必要把iOS开发中的手势识别做一个小小的总结.在上一篇iOS开发之自定义表情键盘(组件封装与自动布局)博客中用到了一个轻击手势,就是在轻击TextView时从表情键盘回到系统键盘,在TextVi ...
- [转载]iOS开发之手势识别
感觉有必要把iOS开发中的手势识别做一个小小的总结.在上一篇iOS开发之自定义表情键盘(组件封装与自动布局)博客中用到了一个轻击手势,就是在轻击TextView时从表情键盘回到系统键盘,在TextVi ...
- iOS开发学习网站汇总
*本文转自CocoaChina 原文:11 Insanely Great iOS Developers Sites永不止步地向他人学习 我相信,要想从一个"还不错"的人变成一个卓越 ...
- iOS开发中六种手势识别
iOS开发中手势识别有六种: 轻击手势(TapGestureRecognizer), 轻扫手势 (SwipeGestureRecognizer), 长按手势(LongPressGestureRecog ...
- 我的iOS开发系列博文
之前目录性的总结了发表过的关于OC方面的文章,今天在目录性的总结一下有关iOS开发的文章.走过路过不要错过哦,今天的博文也全都是干货.写技术博客与大家交流一下思想也是不错的. 下面是我的技术博客中有关 ...
- iOS开发之微信聊天页面实现
在上篇博客(iOS开发之微信聊天工具栏的封装)中对微信聊天页面下方的工具栏进行了封装,本篇博客中就使用之前封装的工具栏来进行聊天页面的编写.在聊天页面中主要用到了TableView的知识,还有如何在俩 ...
- iOS开发之微信聊天工具栏的封装
之前山寨了一个新浪微博(iOS开发之山寨版新浪微博小结),这几天就山寨个微信吧.之前已经把微信的视图结构简单的拖了一下(IOS开发之微信山寨版),今天就开始给微信加上具体的实现功能,那么就先从微信的聊 ...
- 100个iOS开发/设计程序员面试题汇总,你将如何作答?
100个iOS开发/设计程序员面试题汇总,你将如何作答? 雪姬 2015-01-25 19:10:49 工作职场 评论(0) 无论是对于公司还是开发者或设计师个人而言,面试都是一项耗时耗钱的项目, ...
- iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控
-- iOS事件全面解析 概览 iPhone的成功很大一部分得益于它多点触摸的强大功能,乔布斯让人们认识到手机其实是可以不用按键和手写笔直接操作的,这不愧为一项伟大的设计.今天我们就针对iOS的触摸事 ...
随机推荐
- JS实现简易的计算器
JS可以做的事多了,那就用来实现一个计算器吧 看看手机中的计算器,分为普通计算器和科学计算器 自认脑袋不够大,就实现一个普通版本的吧(支持正负数加减乘除等基本连续的运算,未提供括号功能) 看看 ...
- C#+ html 实现类似QQ聊天界面的气泡效果
/**定义两个人的头像*/ Myhead = "<img src=qrc:/chatdemo/Msg/Head.png width='30px'heigth='30px'>&qu ...
- LINQ多个操作嵌套实现
获取集合,需要使用多个条件Where,排序OrderBy,查询Select一起. 先来分步实现: source code: string[] stringArray = { "hgdgh&q ...
- VB.NET Winform的一些功能实现
近段时间,开发的需要,需要写一个winform的程序.用VB.NET来写. 开发开始,需要实现一个窗体设为多文档界面 (MDI) 子窗体的容器.实现这个功能,开始找资料,得知设置一个属性:Form.I ...
- Entity Framework 实体框架的形成之旅--Code First模式中使用 Fluent API 配置(6)
在前面的随笔<Entity Framework 实体框架的形成之旅--Code First的框架设计(5)>里介绍了基于Code First模式的实体框架的经验,这种方式自动处理出来的模式 ...
- Lesson: The "Hello World!" Application
Lesson: The "Hello World!" Application The sections listed below provide detailed instruct ...
- Java并发编程:线程间协作的两种方式:wait、notify、notifyAll和Condition
Java并发编程:线程间协作的两种方式:wait.notify.notifyAll和Condition 在前面我们将了很多关于同步的问题,然而在现实中,需要线程之间的协作.比如说最经典的生产者-消费者 ...
- 阿里巴巴开源技术 WebX
0. WebX项目目前已开源, 项目开源地址:https://github.com/webx/citrus-sample.git 项目参考文档:http://www.openwebx.org/docs ...
- hash简单介绍
hash也称"散列", 是一种基于映射关系的存储方式,将任意长度的二进制值输出为固定长度的较小的二进制值,这种输出的小的固定长度的值为hash值: 1. 散列技术是在关键字key与 ...
- CSS——4种定位
若是没有指定定位方式,默认为静态定位. 1.静态定位(static) 静态定位会将所有元素正常流入页面. 2.绝对定位(absolute) 绝对定位将元素完全从页面流中取出,允许你为他制定一个绝对的位 ...