码迷,mamicode.com
首页 > 移动开发 > 详细

iOS七大手势识别

时间:2016-04-09 12:02:58      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:

也没有什么好说的,方法都差不多,只要记得当你想要同时实现两个或多个手势的话,要遵守<UIGestureRecognizerDelegate>协议,闲言休叙,直接上代码:

  1 #import "RootViewController.h"
  2 #import "RootView.h"
  3 
  4 @interface RootViewController () <UIGestureRecognizerDelegate>  // 手势识别器代理
  5 @property (nonatomic, strong) RootView *rootView;
  6 
  7 @end
  8 
  9 @implementation RootViewController
 10 
 11 - (void)loadView {
 12     self.rootView = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds];
 13     self.view = self.rootView;
 14 }
 15 
 16 
 17 - (void)viewDidLoad {
 18     [super viewDidLoad];
 19     
 20     // 轻拍
 21     [self tapGesture];
 22     
 23     // 平移
 24     //[self panGesture];
 25     
 26     // 缩放(捏合)
 27     [self pinchGesture];
 28     
 29     // 旋转
 30     [self rotationGesture];
 31     
 32     // 长按
 33     [self longPressGesture];
 34     
 35     // 轻扫(滑动)
 36     [self swipeGesture];
 37     
 38     // 屏幕边缘轻扫(滑动)
 39     [self screenEdgePanGesture];
 40 }
 41 
 42 
 43 #pragma mark - 轻拍手势 UITapGestureRecognizer
 44 // 添加轻拍手势
 45 - (void)tapGesture {
 46     // 1.创建手势识别对象
 47     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction:)];
 48     
 49     // 2.将手势添加到相应视图上
 50     [self.rootView.imageView addGestureRecognizer:tap];
 51 }
 52 
 53 // 实现轻拍手势事件
 54 - (void)tapGestureAction:(UITapGestureRecognizer *)tap {
 55     NSLog(@"轻拍成功");
 56 }
 57 
 58 
 59 #pragma mark - 平移手势 UIPanGestureRecognizer
 60 // 添加平移手势
 61 - (void)panGesture {
 62     // 1.创建手势识别对象
 63     UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureAction:)];
 64     
 65     // 2.将手势添加到相应的视图上
 66     [self.rootView.imageView addGestureRecognizer:pan];
 67     
 68 }
 69 
 70 // 实现平移手势事件
 71 - (void)panGestureAction:(UIPanGestureRecognizer *)pan {
 72     
 73     NSLog(@"平移成功");
 74     
 75     // 在view上面挪动的距离
 76     CGPoint p = [pan translationInView:pan.view];
 77     pan.view.transform = CGAffineTransformTranslate(pan.view.transform, p.x, p.y);
 78     
 79     // 清空移动的距离
 80     [pan setTranslation:CGPointZero inView:pan.view];
 81 }
 82 
 83 
 84 #pragma mark - 缩放手势(捏合)UIPinchGestureRecognizer
 85 // 添加平移手势
 86 - (void)pinchGesture {
 87     // 1.创建手势识别对象
 88     UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGestureAction:)];
 89     
 90     // 设置代理
 91     pinch.delegate = self;
 92     
 93     // 2.将手势添加到相应的视图上
 94     [self.rootView.imageView addGestureRecognizer:pinch];
 95     
 96 }
 97 
 98 // 实现缩放手势事件
 99 - (void)pinchGestureAction:(UIPinchGestureRecognizer *)pinch {
100     
101     pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
102     pinch.scale = 1;
103     
104 }
105 
106 
107 #pragma mark - 旋转手势 UIRotationGestureRecognizer
108 // 添加旋转手势
109 - (void)rotationGesture {
110     // 1.创建手势识别对象
111     UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGestureAction:)];
112     
113     // 设置代理
114     rotation.delegate = self;
115     
116     // 2.将手势添加到相应的视图上
117     [self.rootView.imageView addGestureRecognizer:rotation];
118     
119 }
120 
121 // 实现旋转手势事件
122 - (void)rotationGestureAction:(UIRotationGestureRecognizer *)rotation {
123     
124     rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
125     rotation.rotation = 0;
126 }
127 
128 
129 #pragma mark - 长按手势 UILongPressGestureRecognizer
130 // 添加长按手势
131 - (void)longPressGesture {
132     // 1.创建手势识别对象
133     UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureAction:)];
134     
135     // 2.将手势添加到相应的视图上
136     [self.rootView.imageView addGestureRecognizer:longPress];
137 }
138 
139 // 实现长按手势事件
140 - (void)longPressGestureAction:(UILongPressGestureRecognizer *)longPress {
141     // 开始长按的时候执行
142     if (longPress.state == UIGestureRecognizerStateBegan) {
143         NSLog(@"长按成功");
144     }
145 }
146 
147 
148 #pragma mark - 轻扫手势 UISwipeGestureRecognizer
149 // 添加轻扫手势
150 - (void)swipeGesture {
151     // 向上滑动
152     // 1.创建手势识别对象
153     UISwipeGestureRecognizer *upSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureAction:)];
154     upSwipe.direction = UISwipeGestureRecognizerDirectionUp;
155     // 2.将手势添加到相应的视图上
156     [self.rootView.imageView addGestureRecognizer:upSwipe];
157     
158     
159     // 向下滑动
160     // 1.创建手势识别对象
161     UISwipeGestureRecognizer *downSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureAction:)];
162     downSwipe.direction = UISwipeGestureRecognizerDirectionDown;
163     // 2.将手势添加到相应的视图上
164     [self.rootView.imageView addGestureRecognizer:downSwipe];
165     
166     
167     // 向右滑动
168     // 1.创建手势识别对象
169     UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureAction:)];
170     rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
171     // 2.将手势添加到相应的视图上
172     [self.rootView.imageView addGestureRecognizer:rightSwipe];
173     
174     
175     // 向左滑动
176     // 1.创建手势识别对象
177     UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureAction:)];
178     leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
179     // 2.将手势添加到相应的视图上
180     [self.rootView.imageView addGestureRecognizer:leftSwipe];
181 
182 }
183 
184 // 实现轻扫手势事件
185 - (void)swipeGestureAction:(UISwipeGestureRecognizer *)swipe {
186     if (swipe.direction == UISwipeGestureRecognizerDirectionUp) {
187         NSLog(@"向上滑动");
188     }
189     
190     if (swipe.direction == UISwipeGestureRecognizerDirectionDown) {
191         NSLog(@"向下滑动");
192     }
193     
194     if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
195         NSLog(@"向左滑动");
196     }
197     
198     if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
199         NSLog(@"向右滑动");
200     }
201 }
202 
203 
204 #pragma mark - 屏幕边缘轻扫 UIScreenEdgePanGestureRecognizer
205 // 添加屏幕边缘轻扫手势(也有多个方式,我们这里只介绍一下,从屏幕顶部和左边轻扫,其他的都一样)
206 - (void)screenEdgePanGesture {
207     // 从屏幕顶部轻扫屏幕边缘
208     // 1.创建手势识别对象
209     UIScreenEdgePanGestureRecognizer *TopScreenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePanGestureAction:)];
210     TopScreenEdgePan.edges = UIRectEdgeTop;
211     // 2.将手势添加到相应的视图上
212     [self.rootView addGestureRecognizer:TopScreenEdgePan];
213     
214     
215     // 从屏幕左边轻扫屏幕边缘
216     // 1.创建手势识别对象
217     UIScreenEdgePanGestureRecognizer *leftScreenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePanGestureAction:)];
218     leftScreenEdgePan.edges = UIRectEdgeLeft;
219     // 2.将手势添加到相应的视图上
220     [self.rootView addGestureRecognizer:leftScreenEdgePan];
221     
222 }
223 
224 // 实现屏幕边缘轻扫手势
225 - (void)screenEdgePanGestureAction:(UIScreenEdgePanGestureRecognizer *)screenEdgePan {
226     if (screenEdgePan.edges == UIRectEdgeTop) {
227         NSLog(@"从屏幕顶部轻扫屏幕边缘");
228     }
229     
230     if (screenEdgePan.edges == UIRectEdgeLeft) {
231         NSLog(@"从屏幕左边轻扫屏幕边缘");
232     }
233 }
234 
235 
236 #pragma mark - 实现协议方法,同时识别多个手势
237 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
238     return YES;
239 }
240 
241 
242 - (void)didReceiveMemoryWarning {
243     [super didReceiveMemoryWarning];
244     // Dispose of any resources that can be recreated.
245 }
246 @end

 

iOS七大手势识别

标签:

原文地址:http://www.cnblogs.com/zhizunbao/p/5371129.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!