我实现了一个集合视图,例如在链接https://iosrevisited.blogspot.com/2017/09/toggle-between-listviewuitableview-and.html中。
我可以通过点击右上角的图标在网格布局和列表布局之间进行切换,如以上链接的代码所述。
要在布局之间切换,我将在设置布尔值后重新加载集合视图,该布尔值将决定是列表视图/网格视图。但是我没有得到动画(正如Google Drive iOS应用提供的那样)。我尝试了很多事情,例如在UIView.animate中添加collectionView.reloadData(),还尝试了layoutIfNeeded()等。
有人可以给出解决方案,使其随动画一起切换吗?
以下是我的重要collectionview功能:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = view.frame.width
if isListView {
return CGSize(width: width, height: 120)
}else {
return CGSize(width: (width - 15)/2, height: (width - 15)/2)
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 5
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 5
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
}
要在布局之间切换,我只需reloadData()。
答案 0 :(得分:0)
将gridview更改为listview或verca的简便方法。
制作两种布局
private lazy var listCVLayout: UICollectionViewFlowLayout = {
let collectionFlowLayout = UICollectionViewFlowLayout()
collectionFlowLayout.sectionInset = UIEdgeInsets(top: 0, left: 30, bottom: 0, right: 30)
collectionFlowLayout.itemSize = CGSize(width: SCREEN_WIDTH, height: 80)
collectionFlowLayout.minimumInteritemSpacing = 0
collectionFlowLayout.minimumLineSpacing = 0
collectionFlowLayout.scrollDirection = .vertical
return collectionFlowLayout
}()
private lazy var gridCVLayout: UICollectionViewFlowLayout = {
let collectionFlowLayout = UICollectionViewFlowLayout()
collectionFlowLayout.scrollDirection = .vertical
collectionFlowLayout.sectionInset = UIEdgeInsets(top: 0, left: 30, bottom: 0, right: 30)
collectionFlowLayout.itemSize = CGSize(width: (SCREEN_WIDTH - 80) / 2 , height: SCREEN_HEIGHT*0.16)
collectionFlowLayout.minimumInteritemSpacing = 20
collectionFlowLayout.minimumLineSpacing = 20
return collectionFlowLayout
}()
最后在切换视图时使用此
self.listCV.startInteractiveTransition(to: isListView ? self.listCVLayout : self.gridCVLayout, completion: nil)
self.listCV.finishInteractiveTransition()