首页 > 解决方案 > Swift:上一个视图在淡入到具有不同方向的视图时旋转

问题描述

我之前的视图 ( CaptureVC ) 不会自动旋转,因此它只有纵向。我的第二个观点是它过渡到(ManageCaptureVC)确实会自动旋转,并且当它正在淡入淡出过渡到它时,如果设备处于横向状态,CaptureVC 会在淡出时突然旋转。如何避免或补偿这种情况?

更彻底地说,它是一个缩放动画,然后是一个淡入淡出的动画到新的视图控制器(ManageCaptureVC):

UIView.animate(withDuration: 0.3, delay: 0.0, options: UIViewAnimationOptions.curveEaseOut, animations: {
    self.view.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
}, completion: { (finished: Bool) in
    // Here is where CaptureVC rotates while fading into ManageCaptureVC
    let transition = CATransition()
    transition.duration = 0.5
    transition.type = kCATransitionFade
    transition.timingFunction = CAMediaTimingFunction(name:kCAMediaTimingFunctionEaseIn)
    self.present(manageCaptureVC, animated: false, completion: nil)
})

在我的应用程序委托中,我覆盖了这两个函数:

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    if let rootViewController = self.topViewControllerWithRootViewController(rootViewController: window?.rootViewController) {
        if (rootViewController.responds(to: Selector(("canRotate")))) {
            // Unlock landscape view orientations for this view controller if it is not currently being dismissed
            if !rootViewController.isBeingDismissed{
                return .all
            }
        }
    }

    // Only allow portrait (standard behaviour)
    return .portrait
}

private func topViewControllerWithRootViewController(rootViewController: UIViewController!) -> UIViewController? {
    if (rootViewController == nil) {
        return nil
    }
    if (rootViewController.isKind(of: UITabBarController.self)) {
        return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UITabBarController).selectedViewController)
    } else if (rootViewController.isKind(of: UINavigationController.self)) {
        return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UINavigationController).visibleViewController)
    } else if (rootViewController.presentedViewController != nil) {
        return topViewControllerWithRootViewController(rootViewController: rootViewController.presentedViewController)
    }
    return rootViewController
}

我添加了ManageCaptureVC

@objc func canRotate(){}

这样它就可以响应自动旋转。

标签: swiftuiviewcontrollertransformautorotatecatransition

解决方案


推荐阅读