CATransition 在过渡期间变黑

Posted

技术标签:

【中文标题】CATransition 在过渡期间变黑【英文标题】:CATransition fading black during the transition 【发布时间】:2017-04-07 07:06:08 【问题描述】:

我正在两个视图控制器上执行一个简单的从右到左的转换。动画效果完美,正是我想要的结果。但是,由于呈现/呈现的视图控制器淡入/淡出,我在背景中看到黑色闪光。

我的过渡:

let transition = CATransition()
transition.duration = 2.25 // Slow duration to see the black.
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromRight
view.window!.layer.add(transition, forKey: kCATransition)
present(vc, animated: false, completion: nil)

我读到在 AppDelegate didFinishLaunchingWithOptions 中设置窗口颜色可以解决这个问题,但是,这似乎没有做任何事情。 (过渡期间黑色仍然可见。)

self.window!.backgroundColor = UIColor.white

有什么建议可以让两个 VC 在此期间过渡而不会出现黑色闪烁吗?谢谢。

【问题讨论】:

【参考方案1】:

我曾经在prepare(for:sender:) 中进行自定义转换时遇到完全相同的问题。我在阅读A Beginner’s Guide to Animated Custom Segues in ios 8后设法解决了它。

在 Storyboard 中选择您的 segue,选择 custom 类型并应用自定义 SegueFromBottom 类:

这是 Swift 3 的更新代码。

import UIKit

class SegueFromBottom: UIStoryboardSegue

    override func perform()
    
        // Assign the source and destination views to local variables.
        let firstVCView = self.source.view as UIView!
        let secondVCView = self.destination.view as UIView!

        // Get the screen width and height.
        let screenWidth = UIScreen.main.bounds.size.width
        let screenHeight = UIScreen.main.bounds.size.height

        // Specify the initial position of the destination view.
        secondVCView?.frame = CGRect(x: 0.0, y: screenHeight, width: screenWidth, height: screenHeight)

        // Access the app's key window and insert the destination view above the current (source) one.
        let window = UIApplication.shared.keyWindow
        window?.insertSubview(secondVCView!, aboveSubview: firstVCView!)

        // Animate the transition.
        UIView.animate(withDuration: 0.4, animations:  () -> Void in
            firstVCView?.frame = (firstVCView?.frame.offsetBy(dx: 0.0, dy: -screenHeight))!
            secondVCView?.frame = (secondVCView?.frame.offsetBy(dx: 0.0, dy: -screenHeight))!
        , completion:  (Finished) -> Void in
            self.source.present(self.destination as UIViewController, animated: false, completion: nil)
        )
    

【讨论】:

以上是关于CATransition 在过渡期间变黑的主要内容,如果未能解决你的问题,请参考以下文章

CATransition 提前推送过渡替换内容

使用 CATransition 同时动画两个子视图的过渡

CATransition-转场动画

CATransition(转场动画)

CATransition 的延迟启动

IOS 在控制器间跳转实现过渡动画