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手势识别的更多相关文章

  1. iOS控件之UIResponder类

    iOS控件之UIResponder类 在iOS中UIResponder类是专门用来响应用户的操作处理各种事件的,我们知道UIApplication.UIView.UIViewController这几个 ...

  2. UIGestureRecognizer ios手势识别温习

    1.UIGestureRecognizer介绍 手势识别在iOS上非常重要,手势操作移动设备的重要特征,极大的增加了移动设备使用便捷性. iOS系统在3.2以后,为方便开发这使用一些常用的手势,提供了 ...

  3. 【iOS发展-89】UIGestureRecognizer完整的旋转手势识别、缩放和拖拽等效果

    (1)效果 (2)代码 http://download.csdn.net/detail/wsb200514/8261001 (3)总结 --先依据所需创建不同类型的手势识别.比方: UITapGest ...

  4. 触摸事件,手势识别(UITouch,UIGestureRecognizer)

    触摸发生时,UIWindow会有一个队列来存放所有的触摸事件,然后再把这些事件发送给对应的hit-test view,hit-test view会通过touch的四个函数来接收这些事件. 四个函数分别 ...

  5. iOS UIGestureRecognizer与UIMenuController(内容根据iOS编程)

    UIGestureRecognizer 对象会截取本应由视图处理的触摸事件.当某个UIGestureRecognizer对象识别出特定的手势后,就会向指定的对象发送指定的消息.iOS SDK默认提供若 ...

  6. iOS 手势识别器(UIGestureRecognizer)

    UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势. UIGestureRecognizer的子类有: UITapGestureRecogni ...

  7. iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控

    -- iOS事件全面解析 概览 iPhone的成功很大一部分得益于它多点触摸的强大功能,乔布斯让人们认识到手机其实是可以不用按键和手写笔直接操作的,这不愧为一项伟大的设计.今天我们就针对iOS的触摸事 ...

  8. 手势(UIGestureRecognizer)

    通过继承 UIGestureRecognizer 类,实现自定义手势(手势识别器类). PS:自定义手势时,需要 #import <UIKit/UIGestureRecognizerSubcla ...

  9. [ios学习笔记之视图、绘制和手势识别]

    一 视图 二 绘制 三 手势 00:31 UIGestureRecognizer 抽象类 两步 1添加识别器(控制器或者视图来完成) 2手势识别后要做的事情 UIPanGestureRecognize ...

随机推荐

  1. JavaScript Client-Side

    JavaScript Client-Side GET, POST Events, Elements Template, Event, Data(MVC) XMLHttpRequest Logic (f ...

  2. Promise 让异步更优

    每个异步方法都返回一个Promise 更优雅. then方法 每一个Promise  都有一个叫then 的方法, 接受一对callback    被解决时调用,resolve, 被拒绝   reje ...

  3. 初始化rails上的compass项目

    compass以外还有一个很实用的scss模块, _media-queries.scss 通过终端下载 curl -O https://raw.github.com/paranoida/sass-me ...

  4. Light OJ 1013 Love Calculator(DP)

    题目大意: 给你两个字符串A,B 要求一个最短的字符串C,使得A,B同时为C的子串. 问C最短长度是多少? C有多少种? 题目分析: 做这道题目的时候自己并没有推出来,看了网上的题解. 1.dp[C串 ...

  5. 生成树的计数(基尔霍夫矩阵):BZOJ 1002 [FJOI2007]轮状病毒

    1002: [FJOI2007]轮状病毒 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 3928  Solved: 2154[Submit][Statu ...

  6. 【宽搜】BAPC2014 J Jury Jeopardy (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

  7. stl中的空间配置器

    一般我们习惯的c++内存配置如下 class Foo { ... }; Foo* pf = new Foo; delete pf; 这里的new实际上分为两部分执行.首先是先用::operator n ...

  8. zoj 1760 floyd构图+Dinic最大流

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 #include <cstdio> #includ ...

  9. hdoj 1513 Palindrome【LCS+滚动数组】

    Palindrome Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  10. python2.+进化至python3.+ 语法变动差异(不定期更新)

    1.输出 python2.+ 输出: print "" python3.+ 输出: print ("") 2.打开文件 python2.+ 打开文件: file ...