【问题标题】:UICollectionView Doesn't Respond Until Scrolling Completely StopsUICollectionView 在滚动完全停止之前没有响应
【发布时间】:2020-02-11 20:04:29
【问题描述】:

我有一个标准的工作 uicollectionview,我的问题是当我垂直滚动时,uicollectionview 直到滚动完全停止后才会响应任何后续触摸(即停止滚动)。

我能够将问题归结为willDisplayCell 委托方法,在该方法中我使用气泡效果为每个显示单元格设置动画。我很好奇其中是否有什么可以建议我在适当响应触摸的同时仍然可以保留动画的位置?

当我完全注释掉委托方法时,对触摸和滚动的响应非常好。

这是我在 willDisplayCell 委托方法中的代码:

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
    cell.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
    DispatchQueue.main.async {
        let delay = 0.035
        UIView.animate(withDuration: 1, delay: delay, usingSpringWithDamping: 0.75, initialSpringVelocity: 0.8, options: .curveEaseOut, animations: {
            cell.transform = CGAffineTransform(scaleX: 1, y: 1)
            self.view.layoutIfNeeded()
        }, completion: nil)
    }
}

【问题讨论】:

标签: ios swift uicollectionview uicollectionviewcell cgaffinetransform


【解决方案1】:

玩弄了您的代码,似乎问题源于您正在为单元格本身设置动画,但如果您改为为单元格内容设置动画,则集合视图在滚动时响应,同时保持气泡动画。

例如,将所有单元格内容放在单元格内的一个容器视图中,并为该容器视图设置动画,而不是为单元格本身设置动画。

final class Cell: UICollectionViewCell {

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundView = UIView()
        backgroundView?.backgroundColor = .green
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
    guard let cell = cell as? Cell else { return }
    cell.backgroundView?.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
    DispatchQueue.main.async {
        let delay = 0.035
        UIView.animate(withDuration: 1, delay: delay, usingSpringWithDamping: 0.75, initialSpringVelocity: 0.8, options: .curveEaseOut, animations: {
            // you should reset the transform to CGAffineTransform.identity instead of CGAffineTransform(scaleX: 1, y: 1).
            cell.backgroundView?.transform = CGAffineTransform.identity
            // Not sure why you call layoutIfNeeded() here?, but without it the animation is much smoother.
            //self.view.layoutIfNeeded()
        }, completion: nil)
    }
}

【讨论】:

  • 欣赏这个答案,它绝对有助于朝着正确的方向前进! collectionView 肯定响应更快,但有时它仍然会在 collectionview 滚动时,需要多次点击或完全停止交互才能继续。不确定我是否可以提供更多探索或信息来匹配 willDisplay 完全删除时的响应?
猜你喜欢
  • 2017-02-21
  • 2015-10-22
  • 1970-01-01
  • 2013-03-08
  • 1970-01-01
  • 1970-01-01
  • 2020-09-18
  • 2020-04-03
  • 1970-01-01
相关资源
最近更新 更多