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. /dev/random和/dev/urandom的一点备忘

    1.  基本介绍 /dev/random和/dev/urandom是Linux系统中提供的随机伪设备,这两个设备的任务,是提供永不为空的随机字节数据流.很多解密程序与安全应用程序(如SSH Keys, ...

  2. MySQL使用rand函数实现随机数

    sql 的随机函数newID()和RAND() sql server的随机函数newID()和RAND() SELECT * FROM Northwind..Orders ORDER BY NEWID ...

  3. Manacher Ural 1297 Palindrome

    1297. Palindrome Time limit: 1.0 secondMemory limit: 64 MB The “U.S. Robots” HQ has just received a ...

  4. 【模拟】Codeforces 704A & 705C Thor

    题目链接: http://codeforces.com/problemset/problem/704/A http://codeforces.com/problemset/problem/705/C ...

  5. Sqrt(x)——LeetCode

    Implement int sqrt(int x). Compute and return the square root of x. 题目大意:实现求一个int的根. 解题思路:二分. public ...

  6. 【转】Linux 套接字编程中的 5 个隐患

    地址:请点击这里

  7. How to Implement the IContextMenu Interface

    http://msdn.microsoft.com/en-us/library/windows/desktop/hh127443(v=vs.85).aspx IContextMenu is the m ...

  8. 饭卡 (背包01 一维数组) http://acm.hdu.edu.cn/showproblem.php?pid=2546

    /* 从一组数据中选出n个数,使这n个数的和最接近一个值x, 背包问题, 从一系列菜中,从最贵的菜(MAX)之外中选出几个菜,使菜的总价格sum最接近money-5:money-sum-MAX; 钱数 ...

  9. POJ 3280 Cheapest Palindrome(水题)

    题意:给定一个完全由小写字母组成的字符串s,对每个字母比如x(或a,b,c...z),在字符串中添加或者删除它分别需要花费c1['x']和c2['x']的代价,问将给定字符串变成回文串所需要的最少代价 ...

  10. 看小白如何解决ajax跨域问题

    由于此前很少写前端的代码(哈哈,不合格的程序员啊),最近项目中用到json作为系统间交互的手段,自然就伴随着众多ajax请求,随之而来的就是要解决ajax的跨域问题.本篇将讲述一个小白从遇到跨域不知道 ...