翻转后,UIView的框架会发生变化

时间:2017-02-04 09:39:31

标签: ios uiview swift3 uiviewanimationtransition

我使用UIView.transition方法在2个视图之间切换,但之后,两个视图的框架都被更改了。

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 个答案:

答案 0 :(得分:3)

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

解决方案:

将两个视图(即frontView和behindView)放入parentView并使用:

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

}