当动画从两个容器子 UIViews 过渡时,UIViews 在第三次过渡后为空,这是怎么回事?

When Animating a transition from two container child UIViews, the UIViews are empty after the third transition, what's going on?

我有一个使用翻转方法的简单过渡动画,但不知何故在第三次迭代后,动画就变空了。我应该注意,很多教程以编程方式创建一个不可见的 UIView,但我在故事板上创建了一个物理 UIView,并将两个物理容器 UIView 放在空 UIView 的顶部(第一个在顶部)。

然后我做了一个简单的从第一到第二的转换,并且成功了。但是,我反过来做了同样的事情,然后它过渡到一个空的子视图,其余的过渡都是空的。我确定不是,因为我的反向转换语句是错误的,我在正确的转换顺序上添加了打印语句,这些语句出现在控制台中。但是Container UIViews在两次迭代后就消失了,我很困惑,因为我的代码很简单,我不知道还有什么问题...

class ViewController: UIViewController {


@IBOutlet var theView: UIView!

var showingBack = false

@IBAction func actionBtn(sender: UIBarButtonItem) {


    if (showingBack) {
        self.theView.addSubview(firstView)
        UIView.transitionFromView(self.firstView, toView: self.secondView, duration: 1, options: UIViewAnimationOptions.TransitionFlipFromRight, completion: nil)
        showingBack = false
        println("first to second")


    } else {
        self.theView.addSubview(secondView)
        UIView.transitionFromView(self.secondView, toView: self.firstView, duration: 1, options: UIViewAnimationOptions.TransitionFlipFromRight, completion: nil)
        showingBack = true
        println("second to first")

    }

}

@IBOutlet var secondView: UIView!
@IBOutlet var firstView: UIView!

override func viewDidLoad() {
    super.viewDidLoad()

    self.theView.addSubview(firstView)


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

编辑:

所以我还在为这个问题苦苦挣扎。我认为这可能是一个标签问题,所以我给背景涂上了颜色,但似乎整个视图可能从 superview 层次结构中删除,并且再也没有返回......我仍然不知道如何解决这个问题,但就是这样奇怪的。

我设法让它工作了。在您的 IB 中创建一个视图层次结构,如图所示:

然后将您的代码更改为:

import UIKit

class ViewController: UIViewController {

    @IBOutlet var theView: UIView!

    var showingBack = false

    @IBAction func actionBtn(sender: UIBarButtonItem) {

        if (showingBack) {
            self.addSubview(firstView, intoView: theView)
            UIView.transitionFromView(self.secondView, toView: self.firstView, duration: 1, options: UIViewAnimationOptions.TransitionFlipFromRight, completion:{void in
                self.secondView.removeFromSuperview()

            })
            showingBack = false
            println("first to second")


        } else {
            self.addSubview(secondView, intoView: theView)
            UIView.transitionFromView(self.firstView, toView: self.secondView, duration: 1, options: UIViewAnimationOptions.TransitionFlipFromRight, completion:{void in
                self.firstView.removeFromSuperview()

            })
            showingBack = true
            println("second to first")

        }

    }

    @IBOutlet var secondView: UIView!
    @IBOutlet var firstView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.addSubview(firstView, intoView: theView)


    }

    func addSubview(viewToAdd:UIView, intoView superview:UIView) {
        viewToAdd.setTranslatesAutoresizingMaskIntoConstraints(false)
        superview.addSubview(viewToAdd)
        superview.addConstraint(NSLayoutConstraint(item: viewToAdd, attribute: .Leading, relatedBy: .Equal, toItem: superview, attribute: .Leading, multiplier: 1.0, constant: 0.0))
        superview.addConstraint(NSLayoutConstraint(item: viewToAdd, attribute: .Trailing, relatedBy: .Equal, toItem: superview, attribute: .Trailing, multiplier: 1.0, constant: 0.0))
        superview.addConstraint(NSLayoutConstraint(item: viewToAdd, attribute: .Top, relatedBy: .Equal, toItem: superview, attribute: .Top, multiplier: 1.0, constant: 0.0))
        superview.addConstraint(NSLayoutConstraint(item: viewToAdd, attribute: .Bottom, relatedBy: .Equal, toItem: superview, attribute: .Bottom, multiplier: 1.0, constant: 0.0))
    }

}