Я пытаюсь создать коллекцию, которая открывается, когда пользователь проводит пальцем вверх.Сейчас я использую кнопку вместо жеста прокрутки для запуска анимации.Когда анимация начинается, я хочу, чтобы 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!
}