UIView翻转后其框架发生了改变

3
我使用了UIView.transition方法来翻转两个视图,但是在此之后,两个视图的框架都被改变了。
if isFront {
    UIView.transition(from: frontView, to: behindView, duration: 0.5, options: .transitionFlipFromRight,completion: { (finished) in
        if finished {
            self.isFront = false
        }
    })
} else {
    UIView .transition(from: behindView, to: frontView, duration: 0.5, options: .transitionFlipFromLeft, completion: { (finished) in
        if finished {
            self.isFront = true
        }
    })
}

我的问题出在哪里?感谢您的帮助。 image
1个回答

3

我解决了同样的问题。问题在于当我们从视图A到视图B使用翻转过渡时,我们会失去其约束。

解决方法:

将两个视图(即frontView和behindView)放入一个父视图中,并使用以下代码:

UIView.transition(with: scroller, duration: 0.5, options: .transitionFlipFromLeft,animations: { () -> Void in}, completion: { _ in })

例子:

 @IBAction func FlipButtonAction(_ sender: Any) {
    if(front){            
        frontView.isHidden = true
        behindView.isHidden = false

        UIView.transition(with: parentView, duration: 0.5, options: .transitionFlipFromLeft,animations: { () -> Void in}, completion: { _ in })

        print("1")
    }else{
        frontView.isHidden = false
        behindView.isHidden = true

        UIView.transition(with: parentView, duration: 0.5, options: .transitionFlipFromLeft,animations: { () -> Void in}, completion: { _ in })
        print("2")

    }
    front = !front

}

谢谢 @maddy 帮忙检查语法 :) - Amit Sharma

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接