iOS七大手势识别

时间:2022-10-28 21:11:03

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

 #import "RootViewController.h"
#import "RootView.h" @interface RootViewController () <UIGestureRecognizerDelegate> // 手势识别器代理
@property (nonatomic, strong) RootView *rootView; @end @implementation RootViewController - (void)loadView {
self.rootView = [[RootView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.view = self.rootView;
} - (void)viewDidLoad {
[super viewDidLoad]; // 轻拍
[self tapGesture]; // 平移
//[self panGesture]; // 缩放(捏合)
[self pinchGesture]; // 旋转
[self rotationGesture]; // 长按
[self longPressGesture]; // 轻扫(滑动)
[self swipeGesture]; // 屏幕边缘轻扫(滑动)
[self screenEdgePanGesture];
} #pragma mark - 轻拍手势 UITapGestureRecognizer
// 添加轻拍手势
- (void)tapGesture {
// 1.创建手势识别对象
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction:)]; // 2.将手势添加到相应视图上
[self.rootView.imageView addGestureRecognizer:tap];
} // 实现轻拍手势事件
- (void)tapGestureAction:(UITapGestureRecognizer *)tap {
NSLog(@"轻拍成功");
} #pragma mark - 平移手势 UIPanGestureRecognizer
// 添加平移手势
- (void)panGesture {
// 1.创建手势识别对象
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureAction:)]; // 2.将手势添加到相应的视图上
[self.rootView.imageView addGestureRecognizer:pan]; } // 实现平移手势事件
- (void)panGestureAction:(UIPanGestureRecognizer *)pan { NSLog(@"平移成功"); // 在view上面挪动的距离
CGPoint p = [pan translationInView:pan.view];
pan.view.transform = CGAffineTransformTranslate(pan.view.transform, p.x, p.y); // 清空移动的距离
[pan setTranslation:CGPointZero inView:pan.view];
} #pragma mark - 缩放手势(捏合)UIPinchGestureRecognizer
// 添加平移手势
- (void)pinchGesture {
// 1.创建手势识别对象
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGestureAction:)]; // 设置代理
pinch.delegate = self; // 2.将手势添加到相应的视图上
[self.rootView.imageView addGestureRecognizer:pinch]; } // 实现缩放手势事件
- (void)pinchGestureAction:(UIPinchGestureRecognizer *)pinch { pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
pinch.scale = ; } #pragma mark - 旋转手势 UIRotationGestureRecognizer
// 添加旋转手势
- (void)rotationGesture {
// 1.创建手势识别对象
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGestureAction:)]; // 设置代理
rotation.delegate = self; // 2.将手势添加到相应的视图上
[self.rootView.imageView addGestureRecognizer:rotation]; } // 实现旋转手势事件
- (void)rotationGestureAction:(UIRotationGestureRecognizer *)rotation { rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
rotation.rotation = ;
} #pragma mark - 长按手势 UILongPressGestureRecognizer
// 添加长按手势
- (void)longPressGesture {
// 1.创建手势识别对象
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureAction:)]; // 2.将手势添加到相应的视图上
[self.rootView.imageView addGestureRecognizer:longPress];
} // 实现长按手势事件
- (void)longPressGestureAction:(UILongPressGestureRecognizer *)longPress {
// 开始长按的时候执行
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"长按成功");
}
} #pragma mark - 轻扫手势 UISwipeGestureRecognizer
// 添加轻扫手势
- (void)swipeGesture {
// 向上滑动
// 1.创建手势识别对象
UISwipeGestureRecognizer *upSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureAction:)];
upSwipe.direction = UISwipeGestureRecognizerDirectionUp;
// 2.将手势添加到相应的视图上
[self.rootView.imageView addGestureRecognizer:upSwipe]; // 向下滑动
// 1.创建手势识别对象
UISwipeGestureRecognizer *downSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureAction:)];
downSwipe.direction = UISwipeGestureRecognizerDirectionDown;
// 2.将手势添加到相应的视图上
[self.rootView.imageView addGestureRecognizer:downSwipe]; // 向右滑动
// 1.创建手势识别对象
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureAction:)];
rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
// 2.将手势添加到相应的视图上
[self.rootView.imageView addGestureRecognizer:rightSwipe]; // 向左滑动
// 1.创建手势识别对象
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureAction:)];
leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
// 2.将手势添加到相应的视图上
[self.rootView.imageView addGestureRecognizer:leftSwipe]; } // 实现轻扫手势事件
- (void)swipeGestureAction:(UISwipeGestureRecognizer *)swipe {
if (swipe.direction == UISwipeGestureRecognizerDirectionUp) {
NSLog(@"向上滑动");
CGPoint p = CGPointMake(self.rootView.imageView.frame.origin.x, self.rootView.imageView.frame.origin.y - );
self.rootView.imageView.frame = CGRectMake(p.x, p.y, self.rootView.imageView.frame.size.width, self.rootView.imageView.frame.size.height);
} if (swipe.direction == UISwipeGestureRecognizerDirectionDown) {
NSLog(@"向下滑动");
CGPoint p = CGPointMake(self.rootView.imageView.frame.origin.x, self.rootView.imageView.frame.origin.y + );
self.rootView.imageView.frame = CGRectMake(p.x, p.y, self.rootView.imageView.frame.size.width, self.rootView.imageView.frame.size.height);
} if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"向左滑动");
CGPoint p = CGPointMake(self.rootView.imageView.frame.origin.x - , self.rootView.imageView.frame.origin.y);
self.rootView.imageView.frame = CGRectMake(p.x, p.y, self.rootView.imageView.frame.size.width, self.rootView.imageView.frame.size.height);
} if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(@"向右滑动");
CGPoint p = CGPointMake(self.rootView.imageView.frame.origin.x + , self.rootView.imageView.frame.origin.y);
self.rootView.imageView.frame = CGRectMake(p.x, p.y, self.rootView.imageView.frame.size.width, self.rootView.imageView.frame.size.height);
}
} #pragma mark - 屏幕边缘轻扫 UIScreenEdgePanGestureRecognizer
// 添加屏幕边缘轻扫手势(也有多个方式,我们这里只介绍一下,从屏幕顶部和左边轻扫,其他的都一样)
- (void)screenEdgePanGesture {
// 从屏幕顶部轻扫屏幕边缘
// 1.创建手势识别对象
UIScreenEdgePanGestureRecognizer *TopScreenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePanGestureAction:)];
TopScreenEdgePan.edges = UIRectEdgeTop;
// 2.将手势添加到相应的视图上
[self.rootView addGestureRecognizer:TopScreenEdgePan]; // 从屏幕左边轻扫屏幕边缘
// 1.创建手势识别对象
UIScreenEdgePanGestureRecognizer *leftScreenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePanGestureAction:)];
leftScreenEdgePan.edges = UIRectEdgeLeft;
// 2.将手势添加到相应的视图上
[self.rootView addGestureRecognizer:leftScreenEdgePan]; } // 实现屏幕边缘轻扫手势
- (void)screenEdgePanGestureAction:(UIScreenEdgePanGestureRecognizer *)screenEdgePan {
if (screenEdgePan.edges == UIRectEdgeTop) {
NSLog(@"从屏幕顶部轻扫屏幕边缘");
} if (screenEdgePan.edges == UIRectEdgeLeft) {
NSLog(@"从屏幕左边轻扫屏幕边缘");
}
} #pragma mark - 实现协议方法,同时识别多个手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end