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

标签 xcode swift uiview

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

然后我做了一个简单的从第一到第二的转换,它起作用了。但是后来,我以相反的方式做了同样的事情,然后它过渡到一个空的 subview ,其余的过渡都是空的。我确定不是,因为我的反向转换语句是错误的,我在正确的转换顺序上添加了打印语句,这些语句出现在控制台中。但是容器 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.
}

编辑:

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

最佳答案

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

enter image description here

然后将您的代码更改为:

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

}

关于xcode - 当动画从两个容器子 UIViews 过渡时,UIViews 在第三次过渡后为空,这是怎么回事?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31387338/

相关文章:

ios - UIView 添加/删除 subview 与隐藏/显示的速度

swift - Xcode 8.2 Playground 不显示 UIKit 结果

ios - 多种c语言方言

swift - 不同宽度的 UIPageViewController 页面

ios - 快速解析检查用户输入的输入并刷新值

ios - didReceiveRemoteNotification :fetchCompletionHandler: but the completion handler was never called

iPhone iOS 使用 UIView animateWithDuration block 执行操作的正确方法是什么?

ios - 跨多个 MTKViews 共享一个 MTLCommandBuffer

iphone - UILocalNotification 的警报 Action 代码

ios - 多级UIView(xib类型)不触发UITapGestureRecognizer