启动交互式移动并使用自定义布局

时间:2016-09-19 15:47:53

标签: ios swift uicollectionview uicollectionviewcell uicollectionviewlayout

我有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 但我仍然得到相同的结果。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,发现原因是我没有在被覆盖的函数中调用父进程。从您的代码中,我发现您没有从layoutAttributesForElementsInRect函数中的父级检索当前布局属性。您必须在该方法的第一行中执行此操作:var layoutAttributes = super.layoutAttributesForElements(in: rect)