iOS體驗(yàn)性優(yōu)化之RTL適配右滑返回的實(shí)現(xiàn)
簡(jiǎn)述
所謂RTL方向布局就是right to left direction。也就是界面中的元素總是按從右往左的方向進(jìn)行排列布局,大部分國家的書寫以及排列習(xí)慣都是從左往右,是LTR方向布局,而對(duì)于一些阿拉伯國家,文字的書寫以及展示的順序都是從右往左方向的。
iOS的導(dǎo)航支持左滑手勢(shì)返回上一個(gè)界面,這是果粉普遍喜歡的一個(gè)特性,iOS7之后的APP適配大多會(huì)保留這一特性,慢慢的大多用戶已經(jīng)有了這種操作習(xí)慣,對(duì)于iPhone的無虛擬鍵,這種操作也能增加比較友好的用戶體驗(yàn)。
在公司新項(xiàng)目之前,沒有考慮過多語言RTL的適配方案,開始做的時(shí)候UI方面基本實(shí)現(xiàn)用一套布局代碼支持RTL的兩種布局方向。但是真正拿在手里把玩體驗(yàn)時(shí)才真切的感受到?jīng)]有側(cè)滑返回的RTL有多么的不爽。幾經(jīng)查找并沒有找到可參考的合適方案,可能國內(nèi)做多語言適配的技術(shù)圈本身就小,適配RTL的就顯得更加的稀有了。
希望能幫助到有需要的人,或者有更好的思路可以聯(lián)系共同探討。
思路
查不到可參考的資料,只能自己想一想比較合適的方式,恰好在實(shí)現(xiàn)一個(gè)首頁列表跳轉(zhuǎn)詳情頁時(shí)候,解決特殊的轉(zhuǎn)場(chǎng)動(dòng)畫,突然就有了靈感??赡軕?yīng)該有更好的實(shí)現(xiàn)方式,現(xiàn)將我的方式展現(xiàn)給大家。
解決方案
1、關(guān)鍵詞: UIPercentDrivenInteractiveTransition finishInteractiveTransition cancelInteractiveTransition
2、關(guān)鍵方法:updateInteractiveTransition:
3、實(shí)現(xiàn)方式:暫時(shí)以文字代碼描述,具體可參考之前共享的RTL解決方案,里面有相關(guān)源碼,末尾處會(huì)貼出路徑。
具體實(shí)現(xiàn)
1、處理navigation代理
使用runtime方式或者基類方式,viewdidappea每次設(shè)置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、右滑手勢(shì)添加
基類初始化時(shí),RTL環(huán)境下添加右滑手勢(shì),關(guān)閉左滑手勢(shì),實(shí)現(xiàn)最基本的右滑返回。
Navigation 中實(shí)現(xiàn)
- (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、實(shí)現(xiàn)手勢(shì)交互(重點(diǎn))
基類VC中 增加一個(gè)基礎(chǔ)屬性,保存臨時(shí)轉(zhuǎn)場(chǎng)上下文
@property (strong ,nonatomic)UIPercentDrivenInteractiveTransition *transitonContext;
在VC右滑動(dòng)作觸發(fā)事件中,處理轉(zhuǎn)場(chǎng)動(dòng)畫進(jìn)度
- (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) { // 創(chuàng)建過渡對(duì)象,彈出viewController self.transitonContext = [[UIPercentDrivenInteractiveTransition alloc] init]; [self.navigationController popViewControllerAnimated:YES]; }else if (pan.state == UIGestureRecognizerStateChanged) { // 更新 interactive transition 的進(jìn)度 [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; } }
最后就是實(shí)現(xiàn)自定的各種轉(zhuǎn)場(chǎng)動(dòng)畫了,可以簡(jiǎn)單模仿系統(tǒng)的滑動(dòng)切換轉(zhuǎn)場(chǎng),具體處理在下面VC實(shí)現(xiàn)的方法中,返回一個(gè)處理轉(zhuǎn)場(chǎng)的實(shí)例即可
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
4、貼一個(gè)簡(jiǎn)單的動(dòng)畫處理類
@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
大家或許有更好的處理方案,可以一切探討下。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
IOS Ble藍(lán)牙開發(fā)實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了IOS Ble藍(lán)牙開發(fā)的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12iOS的客戶端菜單功能仿百度糯米/美團(tuán)二級(jí)菜單
我剛好最近在開發(fā)一個(gè)商城項(xiàng)目,實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的控件,控件的效果就是類似百度糯米或者美團(tuán)的二級(jí)菜單,非常不錯(cuò)具有參考借鑒價(jià)值,對(duì)百度糯米 美團(tuán)二級(jí)菜單功能感興趣的朋友一起看看吧2016-11-11iOS開發(fā)中使用UIWebView 屏蔽 alert警告框
這篇文章主要介紹了iOS開發(fā)中使用UIWebView 屏蔽 alert警告框的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11AVFoundation AVCaptureSession媒體捕捉
這篇文章主要為大家介紹了ios開發(fā)AVFoundation AVCaptureSession媒體捕捉詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10iOS應(yīng)用進(jìn)入后臺(tái)后計(jì)時(shí)器和位置更新停止問題的解決辦法
這篇文章主要介紹了iOS應(yīng)用進(jìn)入后臺(tái)后計(jì)時(shí)器和位置更新停止問題的解決辦法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03UITableView中Cell重用機(jī)制導(dǎo)致內(nèi)容重復(fù)的解決方法
這篇文章主要為大家詳細(xì)介紹了UITableView中Cell重用機(jī)制導(dǎo)致內(nèi)容重復(fù)的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06