代码之家  ›  专栏  ›  技术社区  ›  Nitish

uiCollectionView显示隐藏动画问题

  •  1
  • Nitish  · 技术社区  · 7 年前

    我在隐藏 UICollectionView . show animation可以很好地工作,但是当我执行hide animation时,它会立即隐藏没有动画的集合视图。这是代码:

    @objc func openMenu(sender: UIButton) {
            if sender.tag == 1 {
                self.buttonView.tag = 2
                self.arrow.image = UIImage(named: "arrowUp.png")
                UIView.animate(withDuration: 0.7, animations: {
                    self.moduleView.frame.size.height = UIScreen.main.bounds.size.height - self.frame.size.height
                }, completion: { _ in
                })
            } else {
                self.buttonView.tag = 1
                self.arrow.image = UIImage(named: "arrowDown.png")
                UIView.animate(withDuration: 0.7, animations: {
                    self.moduleView.frame.size.height = 0
                }, completion: { _ in
                })
            }
        }  
    

    输出:

    enter image description here

    奇怪的是,我用一个简单的 UIView 而且效果很好。自下而上的动画效果完美。代码:

    @objc func openMenu(sender: UIButton) {
            if sender.tag == 1 {
                self.buttonView.tag = 2
                self.arrow.image = UIImage(named: "arrowUp.png")
                UIView.animate(withDuration: 0.7, animations: {
                    self.testView.frame.size.height = UIScreen.main.bounds.size.height - self.frame.size.height
                }, completion: { _ in
                })
            } else {
                self.buttonView.tag = 1
                self.arrow.image = UIImage(named: "arrowDown.png")
                UIView.animate(withDuration: 0.7, animations: {
                    self.testView.frame.size.height = 0
                }, completion: { _ in
                })
            }
        }  
    

    输出:

    enter image description here

    问题 :为什么这不起作用 ui集合视图 ?

    初始化:

    ui集合视图:

    self.moduleView = ModulesCollectionView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 0), collectionViewLayout: UICollectionViewLayout())  
    self.parentView.addSubView(self.moduleView)  
    

    UIView:

    self.testView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 0))  
    self.parentView.addSubView(self.testView)
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Keyur Tailor    7 年前

    你需要使用 layoutSubViews() 正确动画的方法。请更改代码如下:

    @objc func openMenu(sender: UIButton) {
        if sender.tag == 1 {
            self.buttonView.tag = 2
            self.arrow.image = UIImage(named: "arrowUp.png")
            UIView.animate(withDuration: 0.7, animations: {
                self.moduleView.frame.size.height = UIScreen.main.bounds.size.height - self.frame.size.height
                // Add this line
                self.moduleView.layoutSubviews()
            }, completion: { _ in
            })
        } else {
            self.buttonView.tag = 1
            self.arrow.image = UIImage(named: "arrowDown.png")
            UIView.animate(withDuration: 0.7, animations: {
                self.moduleView.frame.size.height = 0
                // Add this line
                self.moduleView.layoutSubviews()
            }, completion: { _ in
            })
        }
    }  
    
    推荐文章