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. // Kneading
30. UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] init];
31. [pinchRecognizer addTarget:self action:@selector(pinch:)];
32. [imageView addGestureRecognizer:pinchRecognizer];
33. pinchRecognizer.delegate = self;// Add delegate to gesture recognizer
34.
35. // rotate
36. UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc]
37. initWithTarget:self action:@selector(rotate:)];
38. [imageView addGestureRecognizer:rotationRecognizer];
39. rotationRecognizer.delegate = self;// Add delegate to gesture recognizer
40.
41. // Click on
42. UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
43. initWithTarget:self action:@selector(tap:)];
44. tapRecognizer.numberOfTapsRequired = 3;// Set three consecutive clicks to respond
45. [imageView addGestureRecognizer:tapRecognizer];
46.
47. // Long press
48. UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc]
49. initWithTarget:self action:@selector(longPress:)];
50. [self.view addGestureRecognizer:longPressRecognizer];
51.
52.
53. // slide swipe
54.// UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
55.// swipe.direction = UISwipeGestureRecognizerDirectionRight; // Each object can only listen in one direction
56.//
57.// [self.view addGestureRecognizer:swipe];
58.
59. // If you need to monitor sliding in different directions, you need to understand it differently swipe object
60.
61. for (int i = 0; i < 4; i++) {
62. // Four directions
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. // Drag the 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. // After each kneading, you have to scale Turn into 1, Prevent stacking
95. recognizer.scale = 1;
96.}
97.
98.-(void)rotate:(UIRotationGestureRecognizer *)recognizer{
99.
100. imageView.transform = CGAffineTransformRotate(imageView.transform, recognizer.rotation);
101.
102. // After each rotation, put rotation Turn into 0, Prevent stacking
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. // It's only drawn when it's just detected
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];// Get the location of the long press
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.// return YES So that two gesture operations can be realized at the same time ( Method of delegation )
157.- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
158. return YES;
159.}
160.
161.
162.
163.@end
164.
 

UIGestureRecognizer More articles on gesture recognition

  1. iOS Controls UIResponder class

    iOS Controls UIResponder class stay iOS in UIResponder Class is designed to respond to user actions and handle various events , We know UIApplication.UIView.UIViewController These are a few ...

  2. UIGestureRecognizer ios Gesture recognition review

    1.UIGestureRecognizer Introduce Gesture recognition is in iOS It's very important , Gesture manipulation is an important feature of mobile devices , Greatly increased the convenience of mobile devices . iOS The system is in 3.2 in the future , For the convenience of developing this, use some common gestures , Provides ...

  3. 【iOS Development -89】UIGestureRecognizer Complete rotation gesture recognition 、 Zoom and drag effects

    (1) effect (2) Code http://download.csdn.net/detail/wsb200514/8261001 (3) summary -- First, create different types of gesture recognition according to your needs . example : UITapGest ...

  4. Touch event , Gesture recognition (UITouch,UIGestureRecognizer)

    When touch happens ,UIWindow There will be a queue for all touch events , And then send these events to the corresponding hit-test view,hit-test view Will pass touch To receive these events . The four functions are ...

  5. iOS UIGestureRecognizer And UIMenuController( The content is based on iOS Programming )

    UIGestureRecognizer Object intercepts touch events that should have been handled by the view . When a UIGestureRecognizer After the object recognizes a specific gesture , Will send the specified message to the specified object .iOS SDK By default, if ...

  6. iOS Gesture recognizer (UIGestureRecognizer)

    UIGestureRecognizer Is an abstract class , Defines the basic behavior of all gestures , Use its subclasses to handle specific gestures . UIGestureRecognizer The subclasses of are : UITapGestureRecogni ...

  7. iOS Development series -- Touch event 、 Gesture recognition 、 Shaking Events 、 Headset by wire

    -- iOS Comprehensive analysis of events overview iPhone A large part of its success is due to its multi touch power , Jobs made people realize that mobile phones can actually be operated directly without using buttons and pens , It's a great design . Today we're going to focus on iOS How to touch ...

  8. gesture (UIGestureRecognizer)

    By inheritance  UIGestureRecognizer class , Implement custom gestures ( Gesture recognizer class ). PS: When customizing gestures , need  #import <UIKit/UIGestureRecognizerSubcla ...

  9. [ios View of learning notes 、 Drawing and gesture recognition ]

    One View Two draw 3、 ... and gesture 00:31 UIGestureRecognizer abstract class Two steps 1 Add recognizer ( Controller or view ) 2 What to do after gesture recognition UIPanGestureRecognize ...

Random recommendation

  1. stay VS Custom code snippet in

    This function is not very practical , But after all VS There is a function point , It's better to know a little ! There's a reason why it's not very practical , Because now the powerful VS The editor has countless plug-ins , And there are many powerful plug-ins ! such as Resharper,Code ...

  2. Android QQ The realization of space browsing picture animation special effects (※)

    1 http://blog.csdn.net/yangblocker/article/details/12680247 2 photoview 3 nineoldandroid

  3. Java in SynchronizedMap And ConcurrentHashMap Comparison of

    How to use summary ConcurrentHashMap: Thread safety : It will Hash The barrel is segmented segment, In other words, the large array is divided into several small segments segment, And every little piece segment It's all ...

  4. 【Netty】Netty And ByteBuf

    One . Preface I have learned before Netty In the transmission part , Now go on learning Netty Medium ByteBuf. Two .ByteBuf 2.1 ByteBuf API The data transmitted on the network is in the form of Byte,Java NIO Provides B ...

  5. Android back door GhostCtrl, Perfect control device arbitrary authority and steal user data

    Android The system seems to have become the preferred target for virus writers around the world , Every day, new malware infects more devices .  This time, , Security company trend technology issues a warning , They found a new Android back door --GhostCtrl Ghos ...

  6. Mysql common problem

    ------------------------------------------------ ------------------------------------------------ -- ...

  7. be based on Servlet Of MVC Mode user login instance

    About MVC A simple explanation of the pattern M Model, The model layer , For example, in the login instance , Class used to handle login operations : V View, View layer , For presentation and interaction with users . Use html.js.css.jsp.jQuery And other front-end technologies : ...

  8. Maven The difference between aggregation and integration

    Such as test-parent It's a polymerization project , The packing method is pom.xml test-a yes test-parent One of the moudle modular , The packing method is jar, And inherit from test-parent: test-b yes tes ...

  9. (python) object-oriented

    One . Object-oriented overview To understand object orientation , You need to understand the concept of process orientation first , So what is process oriented programming ? The most representative C The language , The so-called process oriented programming is when doing something , It needs to be done step by step , What's the first step , What's the second step , such ...

  10. Java Basic knowledge points ( One )

    Preface : This essay , Main records Java The basic knowledge of , Whether it's for projects or interviews , I think they are very useful , So it will continue to update ...... 1.Java Access rights of Java There are four kinds of access rights in : Default access .public.p ...