在7.3 / 9/2 + Swift中如何在设备旋转时禁用旋转动画? [英] In 7.3/9/2+ Swift how to disable rotation animation, when device rotates?

查看:351
本文介绍了在7.3 / 9/2 + Swift中如何在设备旋转时禁用旋转动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题严格关于iOS9 +

This question is strictly about iOS9+

假设您有一个普通的现代应用程序(autolayout,storyboard,universal),它允许所有四个轮换位置

Say you have an ordinary modern app (autolayout, storyboard, universal) which does allow all four rotation positions

您希望它以正常方式自动旋转,因此它会改变当用户将设备从横向旋转到纵向时,基于约束的新布局

you want it to autorotate in the normal way, so it will change to your new constraint-based layouts when user rotates device from landscape to portrait

但在用户旋转设备期间,您只想要无动画。你希望它只是点击到新的横向或直立布局。

But you simply want NO animation during the user rotating the device. You want it to just "click" to the new sideways or upright layout.

我能够实现这一目标的唯一方法是添加:

The only way I have been able to achieve this is by adding:

override func viewWillTransitionToSize(size:CGSize,
       withTransitionCoordinator coordinator:UIViewControllerTransitionCoordinator)
    {
    coordinator.animateAlongsideTransition(nil, completion:
        {_ in
        UIView.setAnimationsEnabled(true)
        })
    UIView.setAnimationsEnabled(false)
    super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator);
    }

一个视图控制器,最高或接近-highest VC,用于保存容器视图的其余部分或场景中的任何内容。

to one view controller, a highest or near-highest VC which holds the rest of the container views or whatever in the scene.

这与使用
willRotateToInterfaceOrientation / didRotateFromInterfaceOrientation(两者)的古老想法基本相同现在无法在现代iOS中使用)来开启动画。

This is basically the same ancient idea of using willRotateToInterfaceOrientation/didRotateFromInterfaceOrientation (both now unusable in modern iOS) to turn animations on-off.

但是有很多问题


  • 这不起作用AppWide,它是乱七八糟的,它是基于场景的

  • this does not work AppWide, it is a mess, it is scene-based

似乎非常糟糕,只需关闭所有动画

it seems Very Bad to just turn off all animations

你可以看到各种赛道

这个问题严格关于iOS9 +

This question is strictly about iOS9+

这些天,有没有更好的方法来关闭支持横向/肖像的应用程序中的旋转动画???

These days, Is there any better way to turn off the rotation animations in an app which supports landscape/portrait ???

这个问题严格关于iOS9 +

This question is strictly about iOS9+

推荐答案

你可以用< STRONG>方法swizzling 。

这意味着要在应用程序中的任何视图控制器上更改对viewWillTransitionToSize的调用,以调用genericViewWillTransitionToSize。

这样你就不必在你的应用程序中使用子类或重复代码。

This means to are going to change calls to "viewWillTransitionToSize" on any view controller in your application to call "genericViewWillTransitionToSize" instead.
This way you do not have to use subclass, or repeated code over your application.

有了这种悲伤,你应该非常关心调整,很棒权力来了很大的责任。将课程放在一个你或你之后的下一个程序员将​​知道如何找到它的地方,当他想要将旋转动画返回给视图控制器时。

With that sad, you should be very carful with swizzling, with great power comes great responsibility. Put the class in a place that you, or the next programmer after you, will know how to find it, when he will want to return the rotation animations to view controllers.

extension UIViewController {

    public override static func initialize() {
        struct Static {
            static var token: dispatch_once_t = 0
        }

        dispatch_once(&Static.token) {
            let originalSelector = #selector(UIViewController.viewWillTransitionToSize(_:withTransitionCoordinator:))
            let swizzledSelector = #selector(UIViewController.genericViewWillTransitionToSize(_:withTransitionCoordinator:))

            let originalMethod = class_getInstanceMethod(self, originalSelector)
            let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)

            let didAddMethod = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))

            if didAddMethod {
                class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
            } else {
                method_exchangeImplementations(originalMethod, swizzledMethod);
            }
        }
    }

    // MARK: - Method Swizzling
    func genericViewWillTransitionToSize(size:CGSize,
                                           withTransitionCoordinator coordinator:UIViewControllerTransitionCoordinator)
    {
        self.genericViewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
        coordinator.animateAlongsideTransition(nil, completion:
            {_ in
                UIView.setAnimationsEnabled(true)
        })
        UIView.setAnimationsEnabled(false)
    }
}

这篇关于在7.3 / 9/2 + Swift中如何在设备旋转时禁用旋转动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆