我正在尝试重新排序我的CollectionView单元格。但似乎无法通过UILongPressGestureRecognizer。该州来自Began - >已更改 - >结束。但它不会转到moveItemAtIndexPath函数。 这是我的代码:
func collectionView(collectionView: UICollectionView, moveItemAtIndexPath sourceIndexPath: NSIndexPath,toIndexPath destinationIndexPath: NSIndexPath) {
var temp1 = [self.viewThatIsShown.selectionForView?.pageArray.count]
let temp = temp1.removeAtIndex(sourceIndexPath.item)
temp1.insert(temp, atIndex: destinationIndexPath.item)
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
let layout = self.viewThatIsShown.selectionForView!.rawJSON["pages"].arrayValue[section]["layout"].stringValue
if layout == "video" || layout == "social-media" {
return 1
} else if layout == "pre-list" {
let dataJSON = self.viewThatIsShown.selectionForView!.rawJSON["pages"].arrayValue[section]["data"]
return dataJSON["listitems"].arrayValue.count
}
let dataSet = self.viewThatIsShown.dataForView!.dataArray[section]
return min(dataSet.embeddedItemsArray.count, 2)
}
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(gesture.view!))
case UIGestureRecognizerState.Ended:
collectionView.endInteractiveMovement()
default:
collectionView.cancelInteractiveMovement()
}
也没有显示错误。我想在计算Section中的项目数时我犯了一些错误。但无法弄清楚。 P.S我在UICollectionview中有六个部分,我想用它来拖放部分。
答案 0 :(得分:0)
看到你的UILongPressGestureRecognizer选择器的实现会非常有用,因为这是这个难题的关键部分。但是,一般情况下,您需要观察.began,.changed,.ended状态,如您所述,并更新位置。一种方法是你可以打开gesture.state
并执行以下操作:
switch gesture.state {
case .began:
//Get a reference to the indexPath being pressed
guard let selectedIndexPath = self.collectionView.indexPathForItem(at: gesture.location(in: self.collectionView)) else { break }
//Start interactive movement
self.collectionView.beginInteractiveMovementForItem(at: selectedIndexPath)
case .changed:
//Update position based on new gesture location
self.collectionView.updateInteractiveMovementTargetPosition(gesture.location(in: self.collectionView))
case .ended:
//End interaction with cell. This will cause moveItemAtIndexPath to fire.
self.collectionView.endInteractiveMovement()
case .default:
//Just to cover your bases
self.collectionView.cancelInteractiveMovement()
}