【问题标题】:UICollectionView show hide animation issueUICollectionView 显示隐藏动画问题
【发布时间】:2018-11-23 01:48:38
【问题描述】:

我在隐藏 UICollectionView 时遇到动画问题。显示动画效果很好,但是当我执行隐藏动画时,它会立即隐藏没有动画的集合视图。这是代码:

@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
            })
        }
    }  

输出:

奇怪的是,我用一个简单的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
            })
        }
    }  

输出:

问题:为什么这对 UICollectionView 不起作用?

初始化:

UICollectionView:

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)

【问题讨论】:

  • 只需为 collectionview / cell 提供背景颜色并检查发生了什么
  • @PrashantTukadiya 效果相同。就像我说的,集合视图很快就会消失
  • @PrashantTukadiya:对不起。我之前错了。我在collectionView 上放了一个有领背景。看到 collectionView 正确解散。但是里面的内容(物品)很快就消失了
  • 我认为问题可能是当集合视图边界发生变化时,单元格可能会尝试根据它进行调整。你在 layoutSubview 的方法中有什么吗?
  • @PrashantTukadiya:不。里面什么都没有

标签: ios swift uiview uicollectionview ios-animations


【解决方案1】:

您需要使用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
        })
    }
}  

【讨论】:

  • 把这个here也发上来,这是一个赏金
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-25
  • 2016-02-02
  • 1970-01-01
  • 2010-11-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多