点击时展开UICollectionView及其单元格

我正在尝试制作一个过渡animation,如链接中的演示。 所以当我点击单元格时,它会展开并覆盖整个屏幕。

这里是我的代码(我不得不承认,我不熟悉CollectionView)

import UIKit class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate { @IBOutlet weak var mainDesLabel: UILabel! @IBOutlet weak var collectionView: UICollectionView! @IBOutlet weak var secDesLabel: UILabel! let searchBar = UISearchBar() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. self.collectionView.delegate = self self.collectionView.dataSource = self self.collectionView.backgroundColor = UIColor.clearColor() //////////////////////////////////////////////////////////////////////// self.searchBar.frame = CGRect(x: 175, y: 0, width: 200, height: 50) self.searchBar.searchBarStyle = UISearchBarStyle.Minimal self.searchBar.backgroundColor = UIColor.whiteColor() self.view.addSubview(searchBar) //////////////////////////////////////////////////////////////////////// } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 10 } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as UICollectionViewCell cell.layer.cornerRadius = 5 return cell } func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { return 1 } //Use for size func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { self.collectionView.frame = self.view.bounds let cell = collectionView.cellForItemAtIndexPath(indexPath) cell!.frame = CGRectMake(0, 0, self.view.bounds.width, self.view.bounds.height) } } 

所以我认为使用'didSelectItemAtIndexPath'将有所帮助,但事实certificate, 这样的

想法? 任何帮助将不胜感激!

一旦UICollectionView完成视图布局,就不能只使用didSelectItemAtIndexPath或任何类似的方法来更新UICollectionViewCell的大小。

要更新单元格高度,可以先捕获在didSelectItemAtIndexPath中点击了哪个单元格。 然后,您可以使用sizeForItemAtIndexpath中传递的新单元格来重新加载整个集合视图。 或者,您可以使用reloadItemsAtIndexPaths重新加载特定的单元格,但仍然需要通过sizeForItemAtIndexpath传递单元格的更新大小。

更新我现在看到问题的细节已经更新了你想要的animation。

我曾经做过类似的animation:

  1. 捕获已经在didSelectItemAtIndexPath中点击的单元格。
  2. 将副本视图添加到UIViewContrller,但其框架完全与已被点击的单元格一致。
  3. 然后,animation这个已经添加的视图。 因此给人的印象是细胞是animation的。

任何附加的function也可以写在这个视图中。 因此单元和animation视图的代码也是分开的。

或者你可以做的是扩大项目,并改变其框架与UIAnimation。

当他细胞被挖掘,你可以使用自动布局扩大单元格内的视图,我暗示(剪辑到界限)。

像这样的东西:

 override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { let item = collectionView.cellForItemAtIndexPath(indexPath) as! cellCollectionViewExpress // or whatever you collection view cell class name is. UIView.animateWithDuration(1.0, animations: { self.view.bringSubviewToFront(collectionView) collectionView.bringSubviewToFront(item) item.frame.origin = self.view.frame.origin /// this view origin will be at the top of the scroll content, you'll have to figure this out item.frame.size.width = self.view.frame.width item.frame.size.height = self.view.frame.height }) } 

我会build议你使用UICollectionView控制器,所以一般情况下使用它。