UIGestureRecognizer手势识别
UIGestureRecognizer
1.#import "ViewController.h"
2.
3.@interface ViewController ()<UIGestureRecognizerDelegate>
4.{
5. UIImageView *imageView ;
6. NSInteger flag;
7.}
8.
9.@property (nonatomic,strong) UIView *panView;
10.
11.@end
12.
13.@implementation ViewController
14.
15.- (void)viewDidLoad {
16. [super viewDidLoad];
17.
18. UIImage *image = [UIImage imageNamed:@"DSC00592.JPG"];
19.
20. CGFloat interval = (self.view.frame.size.width - 240) / 2.0;
21.
22. imageView = [[UIImageView alloc] init];
23. imageView.frame = CGRectMake(interval, 230, 240, 160);
24.
25. imageView.image = image;
26.
27. imageView.userInteractionEnabled = YES;
28.
29. // 捏合
30. UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] init];
31. [pinchRecognizer addTarget:self action:@selector(pinch:)];
32. [imageView addGestureRecognizer:pinchRecognizer];
33. pinchRecognizer.delegate = self;//给手势识别器添加委托
34.
35. // 旋转
36. UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc]
37. initWithTarget:self action:@selector(rotate:)];
38. [imageView addGestureRecognizer:rotationRecognizer];
39. rotationRecognizer.delegate = self;//给手势识别器添加委托
40.
41. // 点击
42. UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
43. initWithTarget:self action:@selector(tap:)];
44. tapRecognizer.numberOfTapsRequired = 3;//设置连续点击三次才响应
45. [imageView addGestureRecognizer:tapRecognizer];
46.
47. // 长按
48. UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc]
49. initWithTarget:self action:@selector(longPress:)];
50. [self.view addGestureRecognizer:longPressRecognizer];
51.
52.
53. // 滑动 swipe
54.// UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
55.// swipe.direction = UISwipeGestureRecognizerDirectionRight; // 每个对象只能监听一个方向
56.//
57.// [self.view addGestureRecognizer:swipe];
58.
59. //若需要监听不同方向的滑动那么需要不同懂swipe对象
60.
61. for (int i = 0; i < 4; i++) {
62. // 四个方向
63. UISwipeGestureRecognizerDirection direction[] = {
64. UISwipeGestureRecognizerDirectionDown,
65. UISwipeGestureRecognizerDirectionLeft,
66. UISwipeGestureRecognizerDirectionUp,
67. UISwipeGestureRecognizerDirectionRight
68. };
69. UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
70. swipe.direction = direction[i];
71.
72. [self.view addGestureRecognizer:swipe];
73. }
74.
75. // 拖动 pan
76.
77. _panView = [[UIView alloc] initWithFrame:CGRectMake(30, 100, 30, 30)];
78. _panView.backgroundColor = [UIColor purpleColor];
79. [self.view addSubview:_panView];
80.
81. UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
82. [_panView addGestureRecognizer:pan];
83.
84.
85. [self.view addSubview:imageView];
86.
87.}
88.
89.-(void)pinch:(UIPinchGestureRecognizer *)recognizer{
90.
91. imageView.transform = CGAffineTransformScale(imageView.transform,
92. recognizer.scale,
93. recognizer.scale);
94. // 每次捏合之后都要将scale变为1,防止叠加
95. recognizer.scale = 1;
96.}
97.
98.-(void)rotate:(UIRotationGestureRecognizer *)recognizer{
99.
100. imageView.transform = CGAffineTransformRotate(imageView.transform, recognizer.rotation);
101.
102. // 每次旋转后都要将rotation变为0,防止叠加
103. recognizer.rotation = 0;
104.}
105.
106.-(void)tap:(UITapGestureRecognizer *)recognizer{
107.
108. [imageView removeFromSuperview];
109.
110.}
111.
112.-(void)longPress:(UILongPressGestureRecognizer *)recognizer{
113.
114. // 只有在刚监测到长按时才绘制
115. if (recognizer.state == UIGestureRecognizerStateBegan) {
116. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(random() % 300, rand() % 600, 50, 50)];
117. view.backgroundColor = [UIColor orangeColor];
118. CGPoint point = [recognizer locationInView:self.view];//获取长按的位置
119. view.center = point;
120. [self.view addSubview:view];
121. }
122.
123.}
124.
125.-(void)swipe:(UISwipeGestureRecognizer *)recognizer{
126. switch (recognizer.direction) {
127. case UISwipeGestureRecognizerDirectionUp:{
128. NSLog(@"up");
129. }
130. break;
131.
132. case UISwipeGestureRecognizerDirectionLeft:{
133. NSLog(@"left");
134. }
135. break;
136. case UISwipeGestureRecognizerDirectionDown:{
137. NSLog(@"down");
138. }
139. break;
140. case UISwipeGestureRecognizerDirectionRight:{
141. NSLog(@"right");
142. }
143. break;
144. default:
145. break;
146. }
147.}
148.
149.-(void)pan:(UIPanGestureRecognizer *)recognizer{
150. CGPoint point = [recognizer locationInView:self.view];
151. _panView.center = point;
152.}
153.
154.
155.
156.// 返回YES使得两个手势操作可以同时实现 (委托方法)
157.- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
158. return YES;
159.}
160.
161.
162.
163.@end
164.
UIGestureRecognizer手势识别的更多相关文章
- iOS控件之UIResponder类
iOS控件之UIResponder类 在iOS中UIResponder类是专门用来响应用户的操作处理各种事件的,我们知道UIApplication.UIView.UIViewController这几个 ...
- UIGestureRecognizer ios手势识别温习
1.UIGestureRecognizer介绍 手势识别在iOS上非常重要,手势操作移动设备的重要特征,极大的增加了移动设备使用便捷性. iOS系统在3.2以后,为方便开发这使用一些常用的手势,提供了 ...
- 【iOS发展-89】UIGestureRecognizer完整的旋转手势识别、缩放和拖拽等效果
(1)效果 (2)代码 http://download.csdn.net/detail/wsb200514/8261001 (3)总结 --先依据所需创建不同类型的手势识别.比方: UITapGest ...
- 触摸事件,手势识别(UITouch,UIGestureRecognizer)
触摸发生时,UIWindow会有一个队列来存放所有的触摸事件,然后再把这些事件发送给对应的hit-test view,hit-test view会通过touch的四个函数来接收这些事件. 四个函数分别 ...
- iOS UIGestureRecognizer与UIMenuController(内容根据iOS编程)
UIGestureRecognizer 对象会截取本应由视图处理的触摸事件.当某个UIGestureRecognizer对象识别出特定的手势后,就会向指定的对象发送指定的消息.iOS SDK默认提供若 ...
- iOS 手势识别器(UIGestureRecognizer)
UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势. UIGestureRecognizer的子类有: UITapGestureRecogni ...
- iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控
-- iOS事件全面解析 概览 iPhone的成功很大一部分得益于它多点触摸的强大功能,乔布斯让人们认识到手机其实是可以不用按键和手写笔直接操作的,这不愧为一项伟大的设计.今天我们就针对iOS的触摸事 ...
- 手势(UIGestureRecognizer)
通过继承 UIGestureRecognizer 类,实现自定义手势(手势识别器类). PS:自定义手势时,需要 #import <UIKit/UIGestureRecognizerSubcla ...
- [ios学习笔记之视图、绘制和手势识别]
一 视图 二 绘制 三 手势 00:31 UIGestureRecognizer 抽象类 两步 1添加识别器(控制器或者视图来完成) 2手势识别后要做的事情 UIPanGestureRecognize ...
随机推荐
- 使用 getNextException() 来检索已经过批处理的特定元素的异常。 ERRORCODE=-4228, SQLSTATE=null
今天查询了一天发现的问题,用ibatis做批量操作时,报错: [非原子批处理出现故障]使用 getNextException() 来检索已经过批处理的特定元素的异常. ERRORCODE=-4228, ...
- JavaScript+CSS实现经典的树形导航栏
在一些管理系统里面,一般右侧都会有树形的导航栏,点击一下就会出现下拉菜单,显示出来该父菜单下面的子菜单 项目,然后配以图片,和CSS的效果,可以说是非常常用的功能,现在做一个项目,正好用到这个功能,于 ...
- java学习之常量与进制
java中的常量包括以下几类: 1.整型常量,比如:3,5,89,99 2.浮点型常量:比如1.23,5.98,3.1415926 3,字符常量:'a','c','1'(需要注意的一点是字符常量只能包 ...
- 从ulimit命令看socket的限制
从ulimit命令看socket的限制 在Linux下面部署应用的时候,有时候会遇上Socket/File: Can’t open so many files的问题,比如还有Squid做代理,当文 ...
- WordPress /wp-admin/users.php畸形s参数路径泄漏漏洞
漏洞版本: WordPress 2.7.x WordPress 2.8.x WordPress 2.9.x WordPress 3.0.x WordPress 3.1.x WordPress 3.2. ...
- 字符串(后缀数组||SAM):NOI2015 品酒大会
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAByIAAALuCAIAAABtq0bwAAAgAElEQVR4nOy9f2wb150vev4boESeln ...
- 如何在IIS6,7中部署ASP.NET网站
http://www.cnblogs.com/fish-li/archive/2012/02/26/2368989.html 阅读目录 开始 查看web.config文件 在IIS中创建网站 IIS6 ...
- 福州大学 Problem 2168 防守阵地 I
http://acm.fzu.edu.cn/problem.php?pid=2168 最重要的是 dp[k]=dp[k-1]-ans[k-1]+x[i]*m; ans[k-1]是m个数求和. Pro ...
- HDOJ(HDU) 2186 悼念512汶川大地震遇难同胞——一定要记住我爱你
Problem Description 当抢救人员发现她的时候,她已经死了,是被垮塌下来的房子压死的,透过那一堆废墟的的间隙可以看到她死亡的姿势,双膝跪着,整个上身向前匍匐着,双手扶着地支撑着身体,有 ...
- Codeforces Round #333 (Div. 1)--B. Lipshitz Sequence 单调栈
题意:n个点, 坐标已知,其中横坐标为为1~n. 求区间[l, r] 的所有子区间内斜率最大值的和. 首先要知道,[l, r]区间内最大的斜率必然是相邻的两个点构成的. 然后问题就变成了求区间[l, ...