iOS七大手势识别

也没有什么好说的,方法都差不多,只要记得当你想要同时实现两个或多个手势的话,要遵守<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         CGPoint p = CGPointMake(self.rootView.imageView.frame.origin.x, self.rootView.imageView.frame.origin.y - 100);
189         self.rootView.imageView.frame = CGRectMake(p.x, p.y, self.rootView.imageView.frame.size.width, self.rootView.imageView.frame.size.height);
190     }
191     
192     if (swipe.direction == UISwipeGestureRecognizerDirectionDown) {
193         NSLog(@"向下滑动");
194         CGPoint p = CGPointMake(self.rootView.imageView.frame.origin.x, self.rootView.imageView.frame.origin.y + 100);
195         self.rootView.imageView.frame = CGRectMake(p.x, p.y, self.rootView.imageView.frame.size.width, self.rootView.imageView.frame.size.height);
196     }
197     
198     if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
199         NSLog(@"向左滑动");
200         CGPoint p = CGPointMake(self.rootView.imageView.frame.origin.x - 100, self.rootView.imageView.frame.origin.y);
201         self.rootView.imageView.frame = CGRectMake(p.x, p.y, self.rootView.imageView.frame.size.width, self.rootView.imageView.frame.size.height);
202     }
203     
204     if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
205         NSLog(@"向右滑动");
206         CGPoint p = CGPointMake(self.rootView.imageView.frame.origin.x + 100, self.rootView.imageView.frame.origin.y);
207         self.rootView.imageView.frame = CGRectMake(p.x, p.y, self.rootView.imageView.frame.size.width, self.rootView.imageView.frame.size.height);
208     }
209 }
210 
211 
212 #pragma mark - 屏幕边缘轻扫 UIScreenEdgePanGestureRecognizer
213 // 添加屏幕边缘轻扫手势(也有多个方式,我们这里只介绍一下,从屏幕顶部和左边轻扫,其他的都一样)
214 - (void)screenEdgePanGesture {
215     // 从屏幕顶部轻扫屏幕边缘
216     // 1.创建手势识别对象
217     UIScreenEdgePanGestureRecognizer *TopScreenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePanGestureAction:)];
218     TopScreenEdgePan.edges = UIRectEdgeTop;
219     // 2.将手势添加到相应的视图上
220     [self.rootView addGestureRecognizer:TopScreenEdgePan];
221     
222     
223     // 从屏幕左边轻扫屏幕边缘
224     // 1.创建手势识别对象
225     UIScreenEdgePanGestureRecognizer *leftScreenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePanGestureAction:)];
226     leftScreenEdgePan.edges = UIRectEdgeLeft;
227     // 2.将手势添加到相应的视图上
228     [self.rootView addGestureRecognizer:leftScreenEdgePan];
229     
230 }
231 
232 // 实现屏幕边缘轻扫手势
233 - (void)screenEdgePanGestureAction:(UIScreenEdgePanGestureRecognizer *)screenEdgePan {
234     if (screenEdgePan.edges == UIRectEdgeTop) {
235         NSLog(@"从屏幕顶部轻扫屏幕边缘");
236     }
237     
238     if (screenEdgePan.edges == UIRectEdgeLeft) {
239         NSLog(@"从屏幕左边轻扫屏幕边缘");
240     }
241 }
242 
243 
244 #pragma mark - 实现协议方法,同时识别多个手势
245 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
246     return YES;
247 }
248 
249 
250 - (void)didReceiveMemoryWarning {
251     [super didReceiveMemoryWarning];
252     // Dispose of any resources that can be recreated.
253 }
254 @end

 

转载于:https://www.cnblogs.com/zhizunbao/p/5371129.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值