iOS体验性优化之RTL适配右滑返回的实现

 更新时间:2018年01月02日 10:30:43   作者:苛求帅  
这篇文章主要给大家介绍了关于iOS体验性优化之RTL适配右滑返回实现的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。

简述

所谓RTL方向布局就是right to left direction。也就是界面中的元素总是按从右往左的方向进行排列布局,大部分国家的书写以及排列习惯都是从左往右,是LTR方向布局,而对于一些阿拉伯国家,文字的书写以及展示的顺序都是从右往左方向的。

iOS的导航支持左滑手势返回上一个界面,这是果粉普遍喜欢的一个特性,iOS7之后的APP适配大多会保留这一特性,慢慢的大多用户已经有了这种操作习惯,对于iPhone的无虚拟键,这种操作也能增加比较友好的用户体验。

在公司新项目之前,没有考虑过多语言RTL的适配方案,开始做的时候UI方面基本实现用一套布局代码支持RTL的两种布局方向。但是真正拿在手里把玩体验时才真切的感受到没有侧滑返回的RTL有多么的不爽。几经查找并没有找到可参考的合适方案,可能国内做多语言适配的技术圈本身就小,适配RTL的就显得更加的稀有了。

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

思路

查不到可参考的资料,只能自己想一想比较合适的方式,恰好在实现一个首页列表跳转详情页时候,解决特殊的转场动画,突然就有了灵感。可能应该有更好的实现方式,现将我的方式展现给大家。

解决方案

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

大家或许有更好的处理方案,可以一切探讨下。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

相关文章

  • iOS中时间与时间戳的相互转化实例代码

    iOS中时间与时间戳的相互转化实例代码

    这篇文章主要介绍了iOS中时间与时间戳的相互转化实例代码,非常具有实用价值,需要的朋友可以参考下。
    2017-03-03
  • iOS新功能引导提示界面实例详解

    iOS新功能引导提示界面实例详解

    在开发中,现在很多app更新了新功能时都会给出用户一个提示,以方便用户更好的体验,那么这个功能如何实现的呢?下面通过本文给大家分享iOS新功能引导提示界面实例详解,需要的的朋友参考下吧
    2017-04-04
  • Objective-C的UIStackView常用属性函数学习笔记

    Objective-C的UIStackView常用属性函数学习笔记

    这篇文章主要为大家介绍了Objective-C的UIStackView常用属性函数学习笔记,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • scrollview tableView嵌套解决方案示例

    scrollview tableView嵌套解决方案示例

    这篇文章主要介绍了scrollview tableView嵌套解决方案示例的代码,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • 配置mac启动项的3种方式总结

    配置mac启动项的3种方式总结

    这篇文章主要给大家介绍了关于配置mac启动项的3种方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2018-02-02
  • IOS 长链接与短链接之间的转换

    IOS 长链接与短链接之间的转换

    这篇文章主要介绍了IOS 长链接与短链接之间的转换的相关资料,需要的朋友可以参考下
    2017-06-06
  • iOS实现Pad上菜单弹出界面

    iOS实现Pad上菜单弹出界面

    这篇文章主要为大家详细介绍了iOS实现Pad上菜单弹出界面的相关代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • iOS 更改UILabel某些字体样式方法

    iOS 更改UILabel某些字体样式方法

    本文通过实例代码给大家介绍了iOS 更改UILabel某些字体样式方法,非常不错,具有参考借鉴价值,需要的朋友参考下吧
    2017-03-03
  • iOS Xcode8更新后输出log日志关闭的方法

    iOS Xcode8更新后输出log日志关闭的方法

    今天刚把xcode更新到了xcode8,运行发现好多log输出,怎么关闭呢,不是很清楚,通过查阅相关资料顺利关掉这些log日志,下面小编把方法共享下,需要的朋友参考下
    2016-09-09
  • iOS mobileconfig配置文件进行签名的配置方法

    iOS mobileconfig配置文件进行签名的配置方法

    这篇文章主要介绍了iOS mobileconfig配置文件进行签名的配置方法,给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-02-02

最新评论