简体   繁体   English

CATransition在过渡期间逐渐褪黑

[英]CATransition fading black during the transition

I'm performing a simple right-to-left transition on two View Controllers. 我正在两个视图控制器上执行从右到左的简单过渡。 The animation works perfect, and exactly my desired result. 动画效果完美,正是我想要的结果。 However, due to the presenting / presented view controllers fading in / out, I get a black flash in the background. 但是,由于呈现/呈现的视图控制器淡入/淡出,我在后台出现黑色闪烁。

My transition: 我的过渡:

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)

I read that setting the window color in AppDelegate didFinishLaunchingWithOptions can fix this, however, that doesn't appear to be doing anything. 我读到,在AppDelegate didFinishLaunchingWithOptions中设置窗口颜色可以解决此问题,但是,这似乎没有任何作用。 (The black is still visible during the transition.) (在过渡期间,黑色仍然可见。)

self.window!.backgroundColor = UIColor.white

Any advise on getting the two VC's to transition without the black flicker during the duration? 有什么建议可以使两个VC在过渡期间没有黑色闪烁的情况下过渡吗? Thanks. 谢谢。

I used to have exactly the same problem when doing custom transitions like you did above inside prepare(for:sender:) . 像您在prepare(for:sender:)内部所做的那样,进行自定义转换时,我曾经遇到过完全相同的问题。 I managed to solve it after reading A Beginner's Guide to Animated Custom Segues in iOS 8 . 在阅读iOS 8中的动画自定义动画初学者指南后,我设法解决了该问题。

Inside the Storyboard select your segue, pick custom kind and apply the custom SegueFromBottom class: 在情节提要中选择您的segue,选择自定义种类并应用自定义SegueFromBottom类:

在此处输入图片说明

Here is the updated code for Swift 3. 这是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)
        })
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM