ios - 如何使 UICollectionView 在 App Store 等集合视图单元格内设置动画?
解决方案
我确实创建了带有欲望动画的简单项目,我没有实现与 AppStore 中完全相同的元素位置,但我只是向您展示了基本动画以及如何实现它。我也没有检查何时collectionView
到达终点,我只是用 100 个元素填充它。
我确实补充
GIF
说它的帧速率很低,在模拟器上看起来很好,没有滞后。
AnimatedCollectionViewController.swift
班级
class AnimatedCollectionViewController: UIViewController {
@IBOutlet private weak var collectionView: UICollectionView!
private let identifier = String(describing: AnimatedCollectionViewCell.self)
private var timer: Timer?
override func viewDidLoad() {
super.viewDidLoad()
/// Loading custom `nib`
let nib = UINib(nibName: identifier, bundle: Bundle.main)
collectionView.register(nib, forCellWithReuseIdentifier: identifier)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
/// Starting animation
startAutoScrollCardsCollectionView()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
/// Remove timer from `RunLoop` object
timer?.invalidate()
timer = nil // just in case
}
private func startAutoScrollCardsCollectionView() {
/// This method start timer and fire it immediately
timer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { [unowned self] _ in
let currentOffset = self.collectionView.contentOffset
self.collectionView.setContentOffset(CGPoint(x: currentOffset.x + 5, y: currentOffset.y), animated: true)
}
}
}
// MARK: - UICollectionViewDataSource extension
extension AnimatedCollectionViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 100
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath)
return cell
}
}
// MARK: - UICollectionViewDelegateFlowLayout extension
extension AnimatedCollectionViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 100, height: 100)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 20, left: 0, bottom: 20, right: 0)
}
}
虚拟AnimatedCollectionViewCell.swift
类
class AnimatedCollectionViewCell: UICollectionViewCell {
override func awakeFromNib() {
super.awakeFromNib()
contentView.backgroundColor = UIColor.random
contentView.layer.cornerRadius = 20
}
}
extension CGFloat {
static func random() -> CGFloat {
return CGFloat(arc4random()) / CGFloat(UInt32.max)
}
}
extension UIColor {
static var random: UIColor {
return UIColor(red: .random(),
green: .random(),
blue: .random(),
alpha: 1.0)
}
}
所以基本上正在发生的事情是,我启动Timer
并立即启动它。这个计时器调用completion
0.1 秒,在这个完成内我改变setContentOffset.x
了collectionView
.
您可以从这里开始并将自定义布局添加到单元格的位置,并检查何时collectionView
到达终点。
推荐阅读
- python - 找不到解决方法:恰好需要 1 个参数(给定 2 个)
- javascript - 如何保存文本框中的输入,然后移至下一页?(Javascript/HTML)
- javascript - 类型错误:这是未定义的;无法访问其“道具”属性
- windows - DynamoDb SocketException:尝试对无法访问的网络进行套接字操作
- javascript - Omnifaces beforeunload 在某些情况下不起作用
- html - 我如何在这个 div 中居中
- python - sorted() 如何对列表中的列表起作用
- c++ - 关于搜索和排序算法的问题
- python - 模板未显示在服务器上
- dart - Flutter 确实在几秒钟长的后台工作(Android 中的 Flutter AsyncTask)