我正在尝试创建一个collectionView,当用户向上滑动时该视图会向上滑动。现在,我使用按钮而不是滚动手势来触发动画。动画开始时,我希望collectionView的高度扩大,但单元格保持原样,然后随着用户滚动向上滑动。如何在不移动单元格的情况下为collectionView设置动画?这可能吗?我想让collectionView扩大并显示滚动过程中被掩盖的隐藏单元格而不显示,或者显示collectionView中的空白,而不仅仅是将屏幕上的所有单元格移高。
相关代码的功能
var buttonPressed:Bool = true
@IBAction func changeView(_ sender: Any) {
if buttonPressed{
UIView.animate(withDuration: 0.05){
self.animateCollectionView.frame = CGRect(x: 0, y: 100, width: 600, height: 1000)
// self.animateCollectionView.transform = CGAffineTransform(translationX: 0, y: -20)
self.view.sendSubview(toBack: self.animateCollectionView)
self.buttonPressed = !self.buttonPressed
print("Ive been expanded")
}
}
else{
UIView.animate(withDuration: 0.05){
self.animateCollectionView.frame = CGRect(x: 0, y: 20, width: 600, height: 1000)
// self.animateCollectionView.transform = CGAffineTransform(translationX: 0, y: 20)
self.buttonPressed = !self.buttonPressed
}
}
}
整体代码:
import UIKit
class ViewController: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 200
}
var counter:Int = 0
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = self.animateCollectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! customCell
counter+=1
cell.cellLabel.text = "\(counter)"
cell.backgroundColor = UIColor.orange
return cell
}
@IBOutlet weak var animateCollectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
animateCollectionView.dataSource = self
animateCollectionView.delegate = self
animateCollectionView.backgroundColor = UIColor.blue
animateCollectionView.frame = CGRect(x: 0, y: 100, width: 600, height: 1000)
self.view.sendSubview(toBack: self.animateCollectionView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
var buttonPressed:Bool = true
@IBAction func changeView(_ sender: Any) {
if buttonPressed{
UIView.animate(withDuration: 0.05){
self.animateCollectionView.frame = CGRect(x: 0, y: 100, width: 600, height: 1000)
// self.animateCollectionView.transform = CGAffineTransform(translationX: 0, y: -20)
self.view.sendSubview(toBack: self.animateCollectionView)
self.buttonPressed = !self.buttonPressed
print("Ive been expanded")
}
}
else{
UIView.animate(withDuration: 0.05){
self.animateCollectionView.frame = CGRect(x: 0, y: 20, width: 600, height: 1000)
// self.animateCollectionView.transform = CGAffineTransform(translationX: 0, y: 20)
self.buttonPressed = !self.buttonPressed
}
}
}
}
class customCell :UICollectionViewCell{
@IBOutlet weak var cellLabel: UILabel!
}