横向旋转后推送方向变化

Posted

技术标签:

【中文标题】横向旋转后推送方向变化【英文标题】:Push direction changes after landscape rotation 【发布时间】:2014-02-09 18:30:04 【问题描述】:

我只有横向应用程序。导航效果很好:当我推送一个新视图时,它会通过从左向右滑动来加载和显示。当它被卸载时,从右向左滑动。 但是,当我旋转设备时,情况发生了变化,现在推动是从右到左。 为了面对 ios 7 中不处理旧式推送而不避免滑动视图奇怪的懒惰出现的错误,我使用此代码

- (void)pushViewControllerRetro:(UIViewController *)viewController 
    CATransition *transition = [CATransition animation];
    transition.duration = 0.3;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromBottom;
    [self.view.layer addAnimation:transition forKey:nil];

    [self pushViewController:viewController animated:NO];


- (void)popViewControllerRetro 
    CATransition *transition = [CATransition animation];
    transition.duration = 0.3;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromTop;
    [self.view.layer addAnimation:transition forKey:nil];

    [self popViewControllerAnimated:NO];

为什么?

【问题讨论】:

你的意思是如果你旋转180度? 【参考方案1】:

当您将设备旋转 180 度时,您会从 LandscapeLeft 转到 LandscapeRight ... 左转右,反之亦然。使用 deviceRotation 来定义你的自定义转换

例如第一种情况

transition.type = [UIDevice currentDevice]. orientation == UIDeviceRotationLandscapeLeft ? kCATransitionFromRight : kCATransitionFromLeft;

【讨论】:

好的,抱歉我抄错了代码。看我的编辑。你的解决方案(你的意思是 UIDeviceOrientationLandscapeLeft,你不是吗)禁用了推动的动画......【参考方案2】:

这是正确的解决方案

- (void)pushViewControllerRetro:(UIViewController *)viewController 
    CATransition *transition = [CATransition animation];
    transition.duration = 0.3;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionPush;


     transition.subtype = [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight ?kCATransitionFromBottom : kCATransitionFromTop;
    [self.view.layer addAnimation:transition forKey:nil];

    [self pushViewController:viewController animated:NO];



- (void)popViewControllerRetro 
    CATransition *transition = [CATransition animation];
    transition.duration = 0.3;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionPush;
    transition.subtype = [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight ?kCATransitionFromTop : kCATransitionFromBottom;

    [self.view.layer addAnimation:transition forKey:nil];

    [self popViewControllerAnimated:NO];

【讨论】:

以上是关于横向旋转后推送方向变化的主要内容,如果未能解决你的问题,请参考以下文章

如何将视图从纵向模式旋转到横向模式

仅将一个视图旋转到横向

旋转到横向后,iOS 边界发生变化,然后又回到纵向

从 Xamarin 中的代码推送新页面后如何旋转屏幕

iOS8 - 在 UINavigationController 上推送 UIViewController 并旋转设备后,以前的视图控制器中的大小错误

2d几何推送算法