iOS體驗性優化---RTL適配右滑返回

[TOC]

簡述

iOS的導航支持左滑手勢返回上一個界面,這是果粉普遍喜歡的一個特性,iOS7之后的APP適配大多會保留這一特性,慢慢的大多用戶已經有了這種操作習慣,對于iPhone的無虛擬鍵,這種操作也能增加比較友好的用戶體驗。


在公司新項目之前,沒有考慮過多語言RTL的適配方案,開始做的時候UI方面基本實現用一套布局代碼支持RTL的兩種布局方向。但是真正拿在手里把玩體驗時才真切的感受到沒有側滑返回的RTL有多么的不爽。幾經查找并沒有找到可參考的合適方案,可能國內做多語言適配的技術圈本身就小,適配RTL的就顯得更加的稀有了。

希望能幫助到有需要的人,或者有更好的思路可以聯系共同探討。

思路

查不到可參考的資料,只能自己想一想比較合適的方式,恰好在實現一個首頁列表跳轉詳情頁時候,解決特殊的轉場動畫,突然就有了靈感??赡軕撚懈玫膶崿F方式,現將我的方式展現給大家。

解決方案

  • 1、關鍵詞: UIPercentDrivenInteractiveTransition finishInteractiveTransition cancelInteractiveTransition

  • 2、關鍵方法:updateInteractiveTransition:

  • 3、實現方式:暫時以文字代碼描述,具體可參考之前共享的RTL解決方案,里面有相關源碼,末尾處會貼出路徑。

具體實現

1、處理navigation代理

使用runtime方式或者基類方式,viewdidappea每次設置nav的代理為自己,viewdiddisappear清空代理(Yoins新版中使用RTL框架中的分類)

- (void)RTL_viewWillAppear:(BOOL)animated
{
    [self RTL_viewWillAppear:animated];
    self.navigationController.delegate = self;
}
- (void)RTL_viewWillDisappear:(BOOL)animated
{
    [self RTL_viewWillDisappear:animated];
    if (self.navigationController.delegate == self) {
        self.navigationController.delegate = nil;
    }
}

2、右滑手勢添加

基類初始化時,RTL環境下添加右滑手勢,關閉左滑手勢,實現最基本的右滑返回。

Navigation 中實現

- (void)RTL_ViewWillAppear:(BOOL)animate
{
//    self.view.backgroundColor = [UIColor whiteColor];
//    // Do any additional setup after loading the view.
   if (![[RTLManager appearance]RTL]) {
       self.interactivePopGestureRecognizer.delegate = self;
   }
   
   self.interactivePopGestureRecognizer.enabled = ![[RTLManager appearance]RTL];
   
   [self RTL_ViewWillAppear:animate];
}

3、實現手勢交互(重點)

基類VC中 增加一個基礎屬性,保存臨時轉場上下文

@property (strong ,nonatomic)UIPercentDrivenInteractiveTransition *transitonContext;

在VC右滑動作觸發事件中,處理轉場動畫進度

- (void)handlePanGesture:(UIScreenEdgePanGestureRecognizer *)pan
{
//    NSLog(@"_____%zd-----%zd",self.navigationController.childViewControllers.count,self.navigationController.viewControllers.count);
//    NSLog(@"----%@",NSStringFromCGPoint([pan translationInView:self.view]));
    
    CGFloat progress = ABS([pan translationInView:self.view].x) / (self.view.bounds.size.width * 1.0);
    progress = MIN(1.0, MAX(0.0, progress));
    
    if (pan.state == UIGestureRecognizerStateBegan) {
        // 創建過渡對象,彈出viewController
        self.transitonContext = [[UIPercentDrivenInteractiveTransition alloc] init];
        [self.navigationController popViewControllerAnimated:YES];
        
    }else if (pan.state == UIGestureRecognizerStateChanged) {
        // 更新 interactive transition 的進度
        [self.transitonContext updateInteractiveTransition:progress];
        
    }else if (pan.state == UIGestureRecognizerStateEnded || pan.state == UIGestureRecognizerStateCancelled) {
        // 完成或者取消過渡
        if (progress > 0.5) {
            [self.transitonContext finishInteractiveTransition];
        }
        else {
            [self.transitonContext cancelInteractiveTransition];
        }
        
        self.transitonContext = nil;
    }
}
- (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController{
    if (self.transitonContext) {
        return self.transitonContext;
    }
    else {
        return nil;
    }
}

最后就是實現自定的各種轉場動畫了,可以簡單模仿系統的滑動切換轉場,具體處理在下面VC實現的方法中,返回一個處理轉場的實例即可

- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC

4、貼一個簡單的動畫處理類

@implementation RTLPushAnimation


- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
    return 0.5;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    UIView *container = transitionContext.containerView;
    UIView *tmpV = [fromVC.view snapshotViewAfterScreenUpdates:YES];
    [container addSubview:toVC.view];
    toVC.view.transform = CGAffineTransformMakeTranslation(-toVC.view.bounds.size.width, 0);
    [container addSubview:tmpV];
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        tmpV.transform = CGAffineTransformMakeTranslation(toVC.view.bounds.size.width, 0);
        toVC.view.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        [tmpV removeFromSuperview];
        [transitionContext completeTransition:YES];
    }];
    
    
}
@end

@implementation RTLPopAnimation

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
    return 0.5;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    UIView *container = transitionContext.containerView;
    
    UIView *tmpV = [fromVC.view snapshotViewAfterScreenUpdates:YES];
    [container addSubview:toVC.view];
    toVC.view.transform = CGAffineTransformMakeTranslation(toVC.view.bounds.size.width, 0);
    [container addSubview:tmpV];
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        tmpV.transform = CGAffineTransformMakeTranslation(-toVC.view.bounds.size.width, 0);
        toVC.view.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        [tmpV removeFromSuperview];
        toVC.view.transform = CGAffineTransformIdentity;
        [transitionContext completeTransition:!transitionContext.transitionWasCancelled];
    }];
}
@end

end

大家或許有更好的處理方案,可以一切探討下。


新版項目中使用的RTL還在不斷地完善中,在公司的公開項目里,有需要可以公開出來。

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容