我需要做的是扩大所选单元格的宽度,同时缩短未选中的其余单元格的宽度,以便一次显示所有单元格。
我现在拥有的是:
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
}
我在子类UIView中有一个水平 UICollectionView设置,有30个单元格,它们被平分。
要通过检测手势来选择单元格,我已将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子类,我实际上可以通过以下代码更改所选单元格的宽度:
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
})
}
}
}
但是当然这只能改变一个被选中的细胞。我需要做的是更改所选单元格的宽度,并根据所选单元格重新计算其他单元格的宽度。
再次澄清我的问题,我想做的是:
我应该挂钩什么方法或功能?请提供简单的样本代码,非常感谢。
答案 0 :(得分:1)
我觉得子类化UICollectionViewFlowLayout对于你的问题来说是一个更合适/可扩展的解决方案。
下面我在子类化UICollectionViewLayout
时分享了一些小代码所以你会得到一般的想法。
将totalItem添加到您的子类中。
以及指示正在选择哪个单元格的索引路径。
在您的集合视图中选择一个单元格调用invalidatelayout
后
在您的情况下,您希望每次都计算所有单元格的属性。
这是粗略草图,未经测试可能存在一些计算错误
- (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;
}