如果我們沒有對 navigation 中的 back 按鈕進行自定義,就可以直接使用系統自帶的左滑 pop。但若自定義了 back 按鈕,就需要對 self.navigationController.interactivePopGestureRecognizer 這個屬性進行設置。
#import "AbeViewController.h" @interface AbeViewController ()@end @implementation AbeViewController - (void)viewDidLoad { [super viewDidLoad]; } - (void)viewDidAppear:(BOOL)animated { // 設置滑動回退 __weak typeof(self) weakSelf = self; self.navigationController.interactivePopGestureRecognizer.delegate = weakSelf; // 判斷是否為第一個 view if (self.navigationController && [self.navigationController.viewControllers count] == 1) { self.navigationController.interactivePopGestureRecognizer.enabled = NO; } } #pragma mark - UIGestureRecognizerDelegate - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { return YES; } @end
透過 UIScreenEdgePanGestureRecognizer 建立一個螢幕邊緣左滑手勢,觸發時執行 pop:
- (void)viewDidAppear:(BOOL)animated {
UIScreenEdgePanGestureRecognizer *edgePanGestureRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgePanGesture:)];
edgePanGestureRecognizer.delegate = self;
edgePanGestureRecognizer.edges = UIRectEdgeLeft;
[self.view addGestureRecognizer:edgePanGestureRecognizer];
}
- (void)edgePanGesture:(UIScreenEdgePanGestureRecognizer *)edgePanGestureRecognizer {
[self.navigationController popViewControllerAnimated:YES];
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
if (self.navigationController && [self.navigationController.viewControllers count] == 1) {
return NO;
}
return YES;
}
在 view 的任何位置左移都能觸發 pop,實作時建立一個 UIPanGestureRecognizer 手勢,並在 push 時對當前畫面截圖,營造出跟手返回的過渡動畫效果。
關鍵知識點:
[panGestureRecognizer locationInView:XX] 獲取 pan 手勢的 CGPoint;panGestureRecognizer.state 為手勢狀態;self.interactivePopGestureRecognizer.enabled = NO; 可關閉原生左滑。
以下是完整實作(ABENavViewController 為 UINavigationController 的子類):
#import "ABENavViewController.h"
@interface ABENavViewController ()
@property (strong, nonatomic) UIPanGestureRecognizer *panGestureRecognizer;
@property (strong, nonatomic) UIImageView *backView;
@property (strong, nonatomic) NSMutableArray *backImgs;
@property (assign) CGPoint panBeginPoint;
@property (assign) CGPoint panEndPoint;
@end
@implementation ABENavViewController
- (void)loadView {
[super loadView];
[self initilization];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self loadBaseUI];
}
- (void)initilization {
self.backImgs = [[NSMutableArray alloc] init];
}
- (void)loadBaseUI {
// 原生方法無效
self.interactivePopGestureRecognizer.enabled = NO;
// 設置手勢
self.panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognizerAction:)];
[self.view addGestureRecognizer:self.panGestureRecognizer];
}
#pragma mark - public method
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
// 截圖保存當前畫面
UIGraphicsBeginImageContextWithOptions([UIScreen mainScreen].bounds.size, YES, 1.0);
[[UIApplication sharedApplication].keyWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.backImgs addObject:img];
[super pushViewController:viewController animated:animated];
}
- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
[_backImgs removeLastObject];
return [super popViewControllerAnimated:animated];
}
#pragma mark - private method
- (void)panGestureRecognizerAction:(UIPanGestureRecognizer *)panGestureRecognizer {
if ([self.viewControllers count] == 1) {
return;
}
if (panGestureRecognizer.state == UIGestureRecognizerStateBegan) {
// 記錄滑動開始位置
self.panBeginPoint = [panGestureRecognizer locationInView:[UIApplication sharedApplication].keyWindow];
// 插入上一級視圖截圖
[self insertLastViewFromSuperView:self.view.superview];
} else if (panGestureRecognizer.state == UIGestureRecognizerStateEnded) {
// 記錄滑動結束位置
self.panEndPoint = [panGestureRecognizer locationInView:[UIApplication sharedApplication].keyWindow];
if ((_panEndPoint.x - _panBeginPoint.x) > 50) {
// 滑動超過閾值,執行 pop
[UIView animateWithDuration:0.3 animations:^{
[self moveNavigationViewWithLenght:[UIScreen mainScreen].bounds.size.width];
} completion:^(BOOL finished) {
[self removeLastViewFromSuperView];
[self moveNavigationViewWithLenght:0];
[self popViewControllerAnimated:NO];
}];
} else {
// 未超過閾值,復位
[UIView animateWithDuration:0.3 animations:^{
[self moveNavigationViewWithLenght:0];
}];
}
} else {
// 跟手移動
CGFloat panLength = ([panGestureRecognizer locationInView:[UIApplication sharedApplication].keyWindow].x - _panBeginPoint.x);
if (panLength > 0) {
[self moveNavigationViewWithLenght:panLength];
}
}
}
// 移動視圖界面
- (void)moveNavigationViewWithLenght:(CGFloat)lenght {
self.view.frame = CGRectMake(lenght, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
_backView.alpha = (lenght / [UIScreen mainScreen].bounds.size.width) * 2 / 3 + 0.33;
}
// 插入上一級截圖
- (void)insertLastViewFromSuperView:(UIView *)superView {
if (_backView == nil) {
_backView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
_backView.image = [_backImgs lastObject];
}
[self.view.superview insertSubview:_backView belowSubview:self.view];
}
// 移除上一級截圖
- (void)removeLastViewFromSuperView {
[_backView removeFromSuperview];
_backView = nil;
}
@end
三種方法各有側重:方法一最簡單,適合未自定義 back 按鈕的情況;方法二穩定性好,從螢幕左緣觸發;方法三則能實現最流暢的跟手過渡動畫,體驗最佳,但實作相對複雜。