如何将ImageView包含在自定义Segue中?

时间:2017-03-02 18:12:53

标签: ios swift xcode

我正在尝试创建一个自定义segue,其中动画的顺序为

1st UIViewController - > ImageView - >第二个UIViewController

我的代码有效,但由于某种原因,ImageView是黑色的。我尝试过使用各种图像和文字图像源,但它仍然是黑色的。

以下是一些图片以便更好地理解 -

  

1(因为第一个UIViewController被ImageView取代)

First VC

  

2(因为ImageView被第二个UIViewController取代)

Second VC

/**
 This segue transitions by dragging the destination UIViewController in from the right and replacing the old UIViewController by pushing it off to the left. This Segue also contains an ImageView that is between the two UIViewControllers.
 */
class CustomSegue2: UIStoryboardSegue {

    override func perform() {

        // Assign the source and destination views to local variables.
        let firstView = self.source.view as UIView!
        let secondView = UIImageView()
        let thirdView = self.destination.view as UIView!

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

        // Set properties of secondView
        secondView.frame = CGRect(x: width, y: 0, width: width, height: height)
        secondView.contentMode =  UIViewContentMode.scaleToFill
        secondView.image = #imageLiteral(resourceName: "orangeRedGradient_MESH_tealGreenGradient")

        // Specify the initial position of the destination view.
        let rect = CGRect(x: width + width, y: 0.0, width: width, height: height)
        thirdView?.frame = rect

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


        // Animation the transition.
        UIView.animate(withDuration: 10, animations: { () -> Void in
            firstView?.frame = firstView!.frame.offsetBy(dx: -width-width, dy: 0.0)
            secondView.frame = secondView.frame.offsetBy(dx: -width-width, dy: 0.0)
            thirdView?.frame = thirdView!.frame.offsetBy(dx: -width-width, dy: 0.0)
        }) { (Finished) -> Void in
            self.source.present(self.destination, animated: false, completion: nil)
        }
    }

}

1 个答案:

答案 0 :(得分:0)

secondView永远不会出现,因为您根本不会将其插入界面。

(我会将您的代码整体描述为完全错误 - 如果您想要自定义转换,您应该编写正确的自定义转换动画代码。但这回答了为什么图像视图为“黑色”的问题 - 它不是不是黑色,完全没有。)