带有显示隐藏动画iOS的UIView翻转过渡

时间:2018-08-09 07:07:34

标签: ios swift animation transition flip

我正在将动画从一个UIView切换到另一个UIView,该动画位于第一个UIView的后面并被隐藏。 代码:

UIView.transition(from: mediumFirstView, to: mediumSecondView, 
                  duration: 0.5, options: [.transitionFlipFromRight, 
                                           .showHideTransitionViews]) { _ in
    self.mediumFirstView.isUserInteractionEnabled = false
    self.mediumSecondView.isUserInteractionEnabled = true
}

或者这个:

UIView.transition(with: mediumLimitedView, duration: 0.5, 
                  options: [.transitionFlipFromRight, 
                            .showHideTransitionViews], 
                  animations: {
    self.mediumFirstView.alpha = 0
    self.mediumSecondView.alpha = 1
}) { _ in
    self.mediumFirstView.isUserInteractionEnabled = false
    self.mediumSecondView.isUserInteractionEnabled = true
}

两者的问题是,只有在完全翻转动画后,First UIView才会隐藏,而不是预期的一半。难道我做错了什么?

查看层次结构:

Views hierarchy

2 个答案:

答案 0 :(得分:4)

他们两个都在为我工作。只需替换 mediumFirstView mediumSecondView 位置。

UIView.transition(from: mediumSecondView, to: mediumFirstView, 
              duration: 5.5, options: [.transitionFlipFromRight, 
                                       .showHideTransitionViews]) { _ in
    self.mediumFirstView.isUserInteractionEnabled = false
    self.mediumSecondView.isUserInteractionEnabled = true
}

enter image description here enter image description here

答案 1 :(得分:1)

如果要设置可见性动画

UIView.transition(with: mediumLimitedView, duration: 0.5, 
                  options: [.transitionFlipFromRight, 
                            .showHideTransitionViews], 
                  animations: {
    self.mediumFirstView.alpha = 0
    self.mediumSecondView.alpha = 1
}) { _ in
    self.mediumFirstView.isHidden = true
    self.mediumSecondView.isHidden = false
    self.mediumFirstView.isUserInteractionEnabled = false
    self.mediumSecondView.isUserInteractionEnabled = true
}

如果您只需要更新isHidden-您可以将计划的操作设置为t / 2秒(在您的情况下,t为0.5s秒,因此t / 2 = 0.25s

DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
            [weak self] in
            guard let strongSelf = self else{
                return
            }
            strongSelf.mediumFirstView.isHidden = true
            strongSelf.mediumSecondView.isHidden = false
        }