[英]UICollectionView Cells Reordering
我正在嘗試重新排序我的CollectionView單元格。 但似乎無法通過UILongPressGestureRecognizer。 州從開始 - >改變 - >結束。 但它不會轉到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中的項目數時我犯了一些錯誤。 但無法弄清楚。 PS我在UICollectionview中有六個部分,我想用它來拖放部分。
看到你的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()
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.