Swift:上一个视图在淡入到具有不同方向的视图时旋转

时间:2018-04-30 20:52:04

标签: swift uiviewcontroller transform autorotate catransition

我之前的观点( 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)
})

在我的app委托中,我覆盖了这两个函数:

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(){}

因此它对自动旋转做出响应。

0 个答案:

没有答案