iphone练习之手势识别(双击、捏、旋转、拖动、划动、长按)UITapGestureRecognizer
首先新建一个基于Sigle view Application的项目,名为GestureTest;我的项目结构如下:
往viewController.xib文件里拖动一个imageView,并使覆盖整个屏幕,改动属性为:
viewController.h文件:
[cpp]viewplaincopy
1. #import <UIKit/UIKit.h>
2.
3. @interface ViewController : UIViewController{
4. IBOutlet UIImageView *imageView;
5. }
6. @property (nonatomic,retain)IBOutlet UIImageView *imageView;
7. @end
并使xib文件里的imageView与之连接;
然后是viewController.m文件的实现部分:
[cpp]viewplaincopy
1. @synthesize imageView;
2.
3. CGFloat lastScaleFactor=1;//放大、缩小
4. CGFloat netRotation;//旋转
5. CGPoint netTranslation;//平衡
6. NSArray *images;//图片数组
7. int imageIndex=0;//数组下标
8.
9. - (void)viewDidLoad
10. {
11. //1、创建手势实例,并连接方法handleTapGesture,点击手势
12. UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapGesture:)];
13. //设置手势点击数,双击:点2下
14. tapGesture.numberOfTapsRequired=2;
15. // imageView添加手势识别
16. [imageView addGestureRecognizer:tapGesture];
17. //释放内存
18. [tapGesture release];
19.
20. //2、手势为捏的姿势:按住option按钮配合鼠标来做这个动作在虚拟器上
21. UIPinchGestureRecognizer *pinchGesture=[[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinchGesture:)];
22. [imageView addGestureRecognizer:pinchGesture];//imageView添加手势识别
23. [pinchGesture release];
24.
25. //3、旋转手势:按住option按钮配合鼠标来做这个动作在虚拟器上
26. UIRotationGestureRecognizer *rotateGesture=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(handleRotateGesture:)];
27. [imageView addGestureRecognizer:rotateGesture];
28. [rotateGesture release];
29.
30. //4、拖手势
31. UIPanGestureRecognizer *panGesture=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanGesture:)];
32. // [imageView addGestureRecognizer:panGesture];
33. [panGesture release];
34.
35. //5、划动手势
36. images=[[NSArray alloc]initWithObjects:@"cell.jpg",@"heihua.jpg",@"xuanyi.jpg", nil];
37. //右划
38. UISwipeGestureRecognizer *swipeGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)];
39. [imageView addGestureRecognizer:swipeGesture];
40. [swipeGesture release];
41. //左划
42. UISwipeGestureRecognizer *swipeLeftGesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeGesture:)];
43. swipeGesture.direction=UISwipeGestureRecognizerDirectionLeft;//不设置黑夜是右
44. [imageView addGestureRecognizer:swipeLeftGesture];
45. [swipeLeftGesture release];
46.
47. //6、长按手势
48. UILongPressGestureRecognizer *longpressGesutre=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongpressGesture:)];
49. //长按时间为1秒
50. longpressGesutre.minimumPressDuration=1;
51. //允许15秒中运动
52. longpressGesutre.allowableMovement=15;
53. //所需触摸1次
54. longpressGesutre.numberOfTouchesRequired=1;
55. [imageView addGestureRecognizer:longpressGesutre];
56. [longpressGesutre release];
57.
58. [super viewDidLoad];
59. // Do any additional setup after loading the view, typically from a nib.
60. }
61. //双击屏幕时会调用此方法,放大和缩小图片
62. -(IBAction)handleTapGesture:(UIGestureRecognizer*)sender{
63. //判断imageView的内容模式是否是UIViewContentModeScaleAspectFit,该模式是原比例,按照图片原时比例显示大小
64. if(sender.view.contentMode==UIViewContentModeScaleAspectFit){
65. //把imageView模式改成UIViewContentModeCenter,按照图片原先的大小显示中心的一部分在imageView
66. sender.view.contentMode=UIViewContentModeCenter;
67. }else{
68. sender.view.contentMode=UIViewContentModeScaleAspectFit;
69. }
70. }
71. //捏的手势,使图片放大和缩小,捏的动作是一个连续的动作
72. -(IBAction)handlePinchGesture:(UIGestureRecognizer*)sender{
73. //得到sender捏手势的大小
74. CGFloat factor=[(UIPinchGestureRecognizer*)sender scale];
75. if(factor>1){
76. //图片放大
77. sender.view.transform=CGAffineTransformMakeScale(lastScaleFactor+(factor-1), (lastScaleFactor+(factor-1)));
78.
79. }else{
80. //缩小
81. sender.view.transform=CGAffineTransformMakeScale(lastScaleFactor*factor, lastScaleFactor*factor);
82.
83. }
84. //状态是否结束,如果结束保存数据
85. if(sender.state==UIGestureRecognizerStateEnded){
86. if(factor>1){
87. lastScaleFactor+=(factor-1);
88. }else{
89. lastScaleFactor*=factor;
90. }
91. }
92. }
93. //旋转手势
94. -(IBAction)handleRotateGesture:(UIGestureRecognizer*)sender{
95. //浮点类型,得到sender的旋转度数
96. CGFloat rotation=[(UIRotationGestureRecognizer*)sender rotation];
97. //旋转角度CGAffineTransformMakeRotation
98. CGAffineTransform transform=CGAffineTransformMakeRotation(rotation+netRotation);
99. //改变图像角度
100. sender.view.transform=transform;
101. //状态结束,保存数据
102. if(sender.state==UIGestureRecognizerStateEnded){
103. netRotation+=rotation;
104. }
105.
106. }
107. //拖手势
108. -(IBAction)handlePanGesture:(UIGestureRecognizer*)sender{
109. //得到拖的过程中的xy坐标
110. CGPoint translation=[(UIPanGestureRecognizer*)sender translationInView:imageView];
111. //平移图片CGAffineTransformMakeTranslation
112. sender.view.transform=CGAffineTransformMakeTranslation(netTranslation.x+translation.x, netTranslation.y+translation.y);
113. //状态结束,保存数据
114. if(sender.state==UIGestureRecognizerStateEnded){
115. netTranslation.x+=translation.x;
116. netTranslation.y+=translation.y;
117. }
118.
119. }
120. //划动手势
121. -(IBAction)handleSwipeGesture:(UIGestureRecognizer*)sender{
122. //划动的方向
123. UISwipeGestureRecognizerDirection direction=[(UISwipeGestureRecognizer*) sender direction];
124. //判断是上下左右
125. switch (direction) {
126. case UISwipeGestureRecognizerDirectionUp:
127. NSLog(@"up");
128. break;
129. case UISwipeGestureRecognizerDirectionDown:
130. NSLog(@"down");
131. break;
132. case UISwipeGestureRecognizerDirectionLeft:
133. NSLog(@"left");
134. imageIndex++;//下标++
135. break;
136. case UISwipeGestureRecognizerDirectionRight:
137. NSLog(@"right");
138. imageIndex--;//下标--
139. break;
140. default:
141. break;
142. }
143. //得到不越界不<0的下标
144. imageIndex=(imageIndex<0)?([images count]-1):imageIndex%[images count];
145. //imageView显示图片
146. imageView.image=[UIImage imageNamed:[images objectAtIndex:imageIndex]];
147.
148. }
149. //长按手势
150. -(IBAction)handleLongpressGesture:(UIGestureRecognizer*)sender{
151. //创建警告
152. UIActionSheet *actionSheet=[[UIActionSheet alloc]initWithTitle:@"Image options" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"Save Image",@"Copy", nil];
153. //当前view显示警告
154. [actionSheet showInView:self.view];
155. [actionSheet release];
156. }
157. -(void)dealloc{
158. [images release];
159. [imageView release];
160. [super dealloc];
161. }
iphone练习之手势识别(双击、捏、旋转、拖动、划动、长按)UITapGestureRecognizer的更多相关文章
- iphone手势识别(双击、捏、旋转、拖动、划动、长按)UITapGestureRecognizer
首先新建一个基于Sigle view Application的项目,名为GestureTest;我的项目结构如下: 往viewController.xib文件里拖动一个imageView,并使覆盖整个 ...
- Swift基础--手势识别(双击、捏、旋转、拖动、划动、长按)
// // ViewController.swift // JieUITapGestureRecognizer // // Created by jiezhang on 14-10-4. // ...
- Swift中实现点击、双击、捏、旋转、拖动、划动、长按手势的类和方法介绍
1.UITapGestureRecognizer 点击/双击手势 代码如下: var tapGesture = UITapGestureRecognizer(target: self, action: ...
- iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)
iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势) 1.UIGestureRecognizer介绍 手势识别在iOS上非常重要,手势操作移动设备的重要特征,极大的增加 ...
- ios iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势)
iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势) 转自容芳志大神的博客:http://www.cnblogs.com/stoic/archive/2013/02/27/2940 ...
- Iphone H5上传照片被旋转
最近做项目发现在Iphone下,我们上传图片都会被翻转,最后查阅资料发现,的确是IOS的问题 不说过程,直接解决方法 iOS下,html方式使用<input type="file&qu ...
- 【转】iOS手势识别的详细使用(拖动,缩放,旋转,点击,手势依赖,自定义手势) -- 不错不错
原文网址:http://blog.csdn.net/totogo2010/article/details/8615940 1.UIGestureRecognizer介绍 手势识别在iOS上非常重要,手 ...
- Winform 图片鼠标滚动查看(放大,缩小,旋转,拖动查看)[日常随笔]
方法千千万,我只是其中一笔[通过控制PictureBox来控制图片,图片完全施展在控件中]...几久不做,还真有点陌生! 窗体构造中添加鼠标滚动: /// <summary> /// 窗体 ...
- javascript 手势缩放 旋转 拖动支持:hammer.js
原文: https://cdn.rawgit.com/hammerjs/hammer.js/master/tests/manual/visual.html /*! Hammer.JS - v2.0.4 ...
随机推荐
- hhtml from表单为什么能提交数据
1.html的列表,分为list,table,form. form表单是专门用来提交数据的,即上传数据的.所以form表单默认是必须有提交按钮的,也就是必须要有个button type类型为submi ...
- R语言学习笔记:生成序列(Genenrating Sequences)
R提供了多种生成不同类型序列的方法.如: > x<-1:20 > x [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 ...
- Android Framework------之Input子系统
下面这是基于Android4.2代码的关于Input子系统的笔记.在这篇笔记中,只涉及Android相关的东西,关于Linux内核中对各种输入设备的统一,在本文中不作说明.此外,由于才疏学浅,文中难免 ...
- 【HDOJ】4326 Game
1. 题目描述一个长度为n个队列,每次取队头的4个人玩儿游戏,每个人等概率赢得比赛.胜者任然处在队头,然而败者按照原顺序依次排在队尾.连续赢得m场比赛的玩家赢得最终胜利.求第k个人赢得最终胜利的概率. ...
- sublime打开文件时自动生成并打开.dump文件
GBK Encoding Support 没有安装前打开ASNI格式编码文件会乱码,安装成功重启则可以打开正常 关于.dump文件生成的解释: 当打开一个非utf-8格式且包含汉字的文件时,subli ...
- Ruby Gem命令
Gem是一个管理Ruby库和程序的标准包,它通过Ruby Gem(如 http://rubygems.org/ )源来查找.安装.升级和卸载软件包,非常的便捷. Ruby 1.9.2版本默认已安装Ru ...
- LeeCode Algorithm #3 Longest Substring Without Repeating Characters
一开始以为是不连续的,其实要求子串是连续的.想法:two-pointer O(n)时间,再借助256大小的int数组.两个下标i,j(i<=j).对于i=0,找到最右侧的字符不重复的下标的后一位 ...
- [原]Unity3D深入浅出 - 常用类的成员变量和成员函数(Tranform、Time、Random、Mathf、Input)
Transform的成员变量 Transform的成员函数 Time类,获取和时间相关的信息,可用来计算帧速率,调整时间流逝的速度等. Random类,可用来生成随机数,随机点和旋转. Mathf类提 ...
- UVa 10622 (gcd 分解质因数) Perfect P-th Powers
题意: 对于32位有符号整数x,将其写成x = bp的形式,求p可能的最大值. 分析: 将x分解质因数,然后求所有指数的gcd即可. 对于负数还要再处理一下,负数求得的p必须是奇数才行. #inclu ...
- C# 向共享文件夹上传及下载文件
//第一步建立共享链接 public static bool connectState(string path, string userName, string passWord) { bool Fl ...