iOS 提供了七種常用手勢識別器,用法大同小異。只要記住一個關鍵:若想同時觸發多個手勢,需遵循 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 {
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction:)];
[self.rootView.imageView addGestureRecognizer:tap];
}
- (void)tapGestureAction:(UITapGestureRecognizer *)tap {
NSLog(@"輕拍成功");
}
#pragma mark - 平移手勢 UIPanGestureRecognizer
- (void)panGesture {
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureAction:)];
[self.rootView.imageView addGestureRecognizer:pan];
}
- (void)panGestureAction:(UIPanGestureRecognizer *)pan {
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 {
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchGestureAction:)];
pinch.delegate = self;
[self.rootView.imageView addGestureRecognizer:pinch];
}
- (void)pinchGestureAction:(UIPinchGestureRecognizer *)pinch {
pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
pinch.scale = 1;
}
#pragma mark - 旋轉手勢 UIRotationGestureRecognizer
- (void)rotationGesture {
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationGestureAction:)];
rotation.delegate = self;
[self.rootView.imageView addGestureRecognizer:rotation];
}
- (void)rotationGestureAction:(UIRotationGestureRecognizer *)rotation {
rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
rotation.rotation = 0;
}
#pragma mark - 長按手勢 UILongPressGestureRecognizer
- (void)longPressGesture {
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGestureAction:)];
[self.rootView.imageView addGestureRecognizer:longPress];
}
- (void)longPressGestureAction:(UILongPressGestureRecognizer *)longPress {
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"長按成功");
}
}
#pragma mark - 輕掃手勢 UISwipeGestureRecognizer
- (void)swipeGesture {
UISwipeGestureRecognizer *upSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureAction:)];
upSwipe.direction = UISwipeGestureRecognizerDirectionUp;
[self.rootView.imageView addGestureRecognizer:upSwipe];
UISwipeGestureRecognizer *downSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureAction:)];
downSwipe.direction = UISwipeGestureRecognizerDirectionDown;
[self.rootView.imageView addGestureRecognizer:downSwipe];
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureAction:)];
rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
[self.rootView.imageView addGestureRecognizer:rightSwipe];
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureAction:)];
leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
[self.rootView.imageView addGestureRecognizer:leftSwipe];
}
- (void)swipeGestureAction:(UISwipeGestureRecognizer *)swipe {
if (swipe.direction == UISwipeGestureRecognizerDirectionUp) { NSLog(@"向上滑動"); }
if (swipe.direction == UISwipeGestureRecognizerDirectionDown) { NSLog(@"向下滑動"); }
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) { NSLog(@"向左滑動"); }
if (swipe.direction == UISwipeGestureRecognizerDirectionRight) { NSLog(@"向右滑動"); }
}
#pragma mark - 屏幕邊緣輕掃 UIScreenEdgePanGestureRecognizer
- (void)screenEdgePanGesture {
UIScreenEdgePanGestureRecognizer *TopScreenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePanGestureAction:)];
TopScreenEdgePan.edges = UIRectEdgeTop;
[self.rootView addGestureRecognizer:TopScreenEdgePan];
UIScreenEdgePanGestureRecognizer *leftScreenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePanGestureAction:)];
leftScreenEdgePan.edges = UIRectEdgeLeft;
[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;
}
@end
七種手勢分別是:輕拍(UITapGestureRecognizer)、平移(UIPanGestureRecognizer)、縮放(UIPinchGestureRecognizer)、旋轉(UIRotationGestureRecognizer)、長按(UILongPressGestureRecognizer)、輕掃(UISwipeGestureRecognizer)以及屏幕邊緣輕掃(UIScreenEdgePanGestureRecognizer)。掌握它們的創建與觸發模式,幾乎就能應對絕大多數互動需求。