简体   繁体   English

swift UIView 过渡不翻转视图

[英]swift UIView transition does not flip the view

UIView.transition does not work with a view as a subview UIView.transition 不适用于作为子视图的视图

I have the following simple set up:我有以下简单的设置:

func flipYellowSubView(imageView: UIImageView) {
        let yellowSubView = UIView(frame: imageView.frame)
        yellowSubView.backgroundColor = .yellow
        imageView.addSubview(yellowSubView)
        UIView.transition(with: yellowSubView, duration: 0.5, options: [.transitionFlipFromBottom], animations: {
        }, completion: { (finished: Bool) in
            if finished {
                yellowSubView.removeFromSuperview()
            }
        })
    }

So if I pass an imageView to flipYellowSubView, I expect the yellowSubview to be added, flipped and then removed.因此,如果我将 imageView 传递给flipYellowSubView,我希望添加、翻转然后删除黄色Subview。

What happens is that the flipYellowSubView is added and removed, but no flipping happens.发生的情况是添加和删除了flipYellowSubView,但没有发生翻转。

You can try the code that follows:你可以试试下面的代码:

func flipYellowSubView(imageView: UIImageView) {
    let yellowSubView = UIView(frame: imageView.frame)
    yellowSubView.backgroundColor = .yellow

    imageView.addSubview(yellowSubView)
    UIView.animate(withDuration: 1, animations: {
        yellowSubView.transform = CGAffineTransform(scaleX: 1, y: -1)
    }) { (finished) in
        yellowSubView.removeFromSuperview()
    }
}

The above transition doesn't work if called in a view which was just added.如果在刚刚添加的视图中调用上述转换将不起作用。 If you had the view already in your hierarchy and loaded before, it did work.如果您的层次结构中已经有视图并且之前已加载,那么它确实有效。

Instead of using a transition you can use UIView.animate and transform y scale to get desired result.您可以使用UIView.animate并转换y scale来获得所需的结果,而不是使用过渡。

Hope it helps.希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM