UIView 自下而上的动画问题 [英] UIView bottom to top animation issue

查看:31
本文介绍了UIView 自下而上的动画问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的函数,我用来从上到下动画视图,反之亦然(如果是从上到下的动画,否则是从下到上的动画):

This is a simple function I am using for animating a view from top to bottom and vice versa (if is top to bottom animation and else is bottom to top animation) :

@objc func openMenu(sender: UIButton) {
        if sender.tag == 1 {
            self.buttonView.tag = 2
             self.moduleView = ModulesCollectionView(frame: CGRect(x: 0, y: self.frame.origin.y + self.frame.size.height + 20, width: UIScreen.main.bounds.size.width, height: 0), collectionViewLayout: UICollectionViewLayout())
            self.window?.addSubview(self.moduleView)
            UIView.animate(withDuration: 0.7, animations: {
                self.moduleView.frame = CGRect(x: 0, y: self.frame.origin.y + self.frame.size.height + 20, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height - self.frame.size.height - 22)
            }, completion: { _ in

            })

        } else {
            self.buttonView.tag = 1
            UIView.animate(withDuration: 3, animations: {
                self.moduleView.frame = CGRect(x: 0, y: self.frame.origin.y + self.frame.size.height + 20, width: UIScreen.main.bounds.size.width, height: 0)
            }, completion: { _ in
                self.moduleView.removeFromSuperview()
            })
        }
    }  

顶部动画效果很好,视图在 0.7 秒内从上到下愉快地动画.但是,不会发生自下而上的动画.该视图立即被删除.这是我得到的结果:

Top animation works fine and the view is animated from top to bottom pleasantly in 0.7 seconds. However, bottom to top animation does not happen. The view is removed instantly. This is the result I am getting :

但我也希望动画从下到上保持干净.就像此处.

But I want the animation to be clean while going from bottom to top as well. Just like here.

次要:我最终计划实现的是PullUpController 与完全相反的动画.因此,如果有人知道类似的库(下拉拖动)可以共享那里的输入.

Secondary : What I finally plan to achieve is PullUpController with the exact reverse animation. So if anyone knows a similar library (pull down drag) can share there inputs.

更新:该问题仅出现在 UICollectionView 中.我用一个简单的 UIView 替换了 collectionView,它工作得很好.

推荐答案

您应该尝试在动画块结束后使用 layoutSubViews() 方法.像这样更改动画块.

You should try to use layoutSubViews() method after at the end of animation block. Change the animation block like this.

对于 if 块:

self.moduleView.frame = CGRect(x: 0, y: self.frame.origin.y + self.frame.size.height + 20, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height - self.frame.size.height - 22)
self.moduleView.layoutSubviews()

对于 else 块:

self.moduleView.frame = CGRect(x: 0, y: self.frame.origin.y + self.frame.size.height + 20, width: UIScreen.main.bounds.size.width, height: 0)
self.moduleView.layoutSubviews()

这篇关于UIView 自下而上的动画问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆