【发布时间】:2016-09-19 15:47:52
【问题描述】:
我有一个带有自定义布局的UICollectionview 作为UIViewcontroller 的子视图。我想拖动我的单元格并让集合视图在被拖动的单元格移动到未拖动的单元格的边界时重新排序单元格。当您使用集合视图时会提供此功能。但是,一旦我添加了自己的布局,单元格在启动交互移动后开始消失。
这就是我设置 customFlow 布局的方式:
private var frames = [CGRect(x: 0.0, y: 0.0, width: 40.0, height: 40.0),//0
CGRect(x: 40.0, y: 0.0, width: 40.0, height: 40.0),//1
CGRect(x: 80.0, y: 0.0, width: 40.0, height: 40.0),//2
CGRect(x: 100.0, y: 0.0, width: 40.0, height: 40.0),//3
CGRect(x: 120.0, y: 0.0, width: 40.0, height: 40.0)]//4
private var cache = [NSIndexPath:UICollectionViewLayoutAttributes]()
override func prepareLayout() {
super.prepareLayout()
for item in 0 ..< collectionView!.numberOfItemsInSection(0){
let indexPath = NSIndexPath(forItem: item, inSection: 0)
let attributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)
attributes.frame = frames[item]
cache[indexPath] = attributes
}
}
override func collectionViewContentSize() -> CGSize {
return UIScreen.mainScreen().bounds.size
}
override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var layoutAttributes = [UICollectionViewLayoutAttributes]()
for attributes in cache.values {
if CGRectIntersectsRect(attributes.frame, rect) {
layoutAttributes.append(attributes)
}
}
return layoutAttributes
}
override func layoutAttributesForInteractivelyMovingItemAtIndexPath(indexPath: NSIndexPath, withTargetPosition position: CGPoint) -> UICollectionViewLayoutAttributes {
let attribute = cache[indexPath]
attribute!.center = position
return attribute!
}
}
这就是我启动交互动作的方式:
func handleLongPressForCollection(gesture: UILongPressGestureRecognizer){
switch(gesture.state) {
case UIGestureRecognizerState.Began:
guard let selectedIndexPath = self.CollectionView.indexPathForItemAtPoint(gesture.locationInView(self.collectionView)) else {
break
}
CollectionView.beginInteractiveMovementForItemAtIndexPath(selectedIndexPath)
case UIGestureRecognizerState.Changed:
collectionView.updateInteractiveMovementTargetPosition(gesture.locationInView(CollectionView))
case UIGestureRecognizerState.Ended:
collectionView.endInteractiveMovement()
default:
collectionView.cancelInteractiveMovement()
break
}
}
我不太了解invalidatelayout。我想我需要它来解决这个问题?我试过覆盖函数invalidationContextForEndingInteractiveMovementOfItemsToFinalIndexPaths
但我仍然得到相同的结果。
【问题讨论】:
标签: ios swift uicollectionview uicollectionviewcell uicollectionviewlayout