应该使用什么方法dynamic更改所选UICollectionViewCell的大小?

我需要做的是扩大所选单元格的宽度,同时缩小未选中单元格的宽度,以便一次显示所有单元格。

我现在拥有的是:

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 30 } func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { return CGSize(width: collectionView.bounds.width / 30, height: collectionView.bounds.height) } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { var cell: UIDateSliderCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as UIDateSliderCell return cell } 

我有一个水平的 UICollectionView安装在子类UIView,与30个单元格,他们平分。

要通过检测手势来select一个单元格,我将UIPanGestureRecognizer添加到collectionView。

  func pan(sender: UIPanGestureRecognizer) { switch sender.state { case .Began: fallthrough case .Changed: _collectionView.selectItemAtIndexPath(_collectionView.indexPathForItemAtPoint(CGPointMake(sender.locationInView(_collectionView).x, 0.0)), animated: true, scrollPosition: nil) case .Ended: break default: break } } 

此外,我有UICollectionViewCell subclassed,我实际上可以通过此代码更改所选单元格的宽度:

  override var selected: Bool { didSet { if selected { UIView.animateWithDuration(1.0, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1.0, options: .CurveEaseIn, animations: { () -> Void in self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width*2, self.frame.size.height) }, completion: { (finished: Bool) -> Void in }) } } } 

但是,当然,这只能改变一个被选中的单元格。 我需要做的是改变一个单元格的宽度,并根据所选单元格重新计算其他单元格的宽度。

为了再次澄清我的问题,我想要做的是这样的:

  1. 比方说,UICollectionView.width是300
  2. 它有30个单元,Cell.width是300/30 = 10
  3. 所有单元格必须一次显示,UICollectionView不滚动。
  4. 当一个单元格被选中时,其宽度将是20。
  5. 所以其他细胞的宽度将是(300-20)/ 29。

我应该挂什么方法或function? 请简单的简单示例代码,非常感谢。

我觉得像inheritanceUICollectionViewFlowLayout将是一个更适合您的问题的可扩展解决scheme。

下面我分享UICollectionViewLayout子类时共享小代码所以你会得到一般的想法。

将totalItem添加到您的子类。 和一个指示哪个单元被选中的索引path。 在您的collectionview中select一个cell call invalidatelayout

在你的情况下,你想每次计算所有单元格的属性。

这是粗略的草图,没有经过testing可能有一些计算错误

  - (void)prepareLayout { self.totalItems = (int)[self.collectionView numberOfItemsInSection:0]; } - (CGSize)collectionViewContentSize { return self.collectionView.frame.size; } -(CGRect) rectForIndex:(int) index { //Here you should calculate the sizes of all the cells // The ones before the selected one , the selected one, and the ones after the selected if (index<selected) { //Regular calculating frame=CGRectMake(0, height * index, width, height) } else if (index>selected) { //here the tricky part is the origin you ( assuming one is seleceted) you calculate the accumulated height of index-1 regular cells and 1 selected cell frame=CGRectMake(0, (height* (selected-1) + sizeOfSelectedCell) +height * (index-selected), width, height) } else { frame=CGRectMake(0, (height* (index-selected)) , width, selectedHeight) } return frame; } - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect { NSMutableArray *attributes = [NSMutableArray array]; for (int i = 0; i< self.totalItems; i++) { NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0]; [attributes addObject:[self layoutAttributesForItemAtIndexPath:indexPath]]; } return attributes; } -(UICollectionViewLayoutAttributes *) layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; attributes.frame = [self rectForIndex:(int)indexPath.row]; return attributes; }