Ios 嵌入到容器视图中时,UICollectionViewController重新排序不起作用
当我将它添加到我的Ios 嵌入到容器视图中时,UICollectionViewController重新排序不起作用,ios,swift,uicollectionview,ios9,Ios,Swift,Uicollectionview,Ios9,当我将它添加到我的UICollectionViewController子类时,在iOS9中重新排序是有效的 override func collectionView(collectionView: UICollectionView, moveItemAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) 当此UICollection
UICollectionViewController
子类时,在iOS9中重新排序是有效的
override func collectionView(collectionView: UICollectionView,
moveItemAtIndexPath sourceIndexPath: NSIndexPath,
toIndexPath destinationIndexPath: NSIndexPath)
当此UICollectionViewController
子类嵌入到容器视图中时,它不起作用
我已经演示了这个问题
关于为什么或如何修复它,您有什么想法吗?这里的问题是您将UICollectionView放在UIViewController中的UIContainerView中。这只需要再执行几个步骤,UICollectionView就可以按预期工作 将以下内容添加到CollectionViewController中的ViewDidLoad:
self.collectionView!.addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: "handleLongGesture:"))
然后将以下函数添加到CollectionViewController:
func handleLongGesture(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(gesture.view!))
case UIGestureRecognizerState.Ended:
collectionView!.endInteractiveMovement()
default:
collectionView!.cancelInteractiveMovement()
}
}
最后,请确保包括以下内容,以确保正确处理数据源:
override func collectionView(collectionView: UICollectionView, moveItemAtIndexPath sourceIndexPath: NSIndexPath,toIndexPath destinationIndexPath: NSIndexPath) {
// Swap the values of the source and destination
}
查看此以了解更多有关此的信息
希望这对你有所帮助。Scooter的答案是正确的! 以下是Swift 3语法版本:
import UIKit
class ViewController: UIViewController
{
// MARK: - IBOutlets
@IBOutlet weak var uiCollectionView: UICollectionView!
// MARK: - Lifecycle
override func viewDidLoad()
{
super.viewDidLoad()
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.handleLongGesture))
self.uiCollectionView.addGestureRecognizer(longPressGesture)
}
// MARK: - Gesture recogniser
@objc func handleLongGesture(gesture: UILongPressGestureRecognizer)
{
switch(gesture.state)
{
case .began:
guard let selectedIndexPath = self.uiCollectionView.indexPathForItem(at: gesture.location(in: self.uiCollectionView)) else
{
break
}
self.uiCollectionView.beginInteractiveMovementForItem(at: selectedIndexPath)
case .changed:
self.uiCollectionView.updateInteractiveMovementTargetPosition(gesture.location(in: gesture.view!))
case .ended:
self.uiCollectionView.endInteractiveMovement()
default:
self.uiCollectionView.cancelInteractiveMovement()
}
}
}
// MARK: - UICollectionViewDataSource
extension ViewController: UICollectionViewDataSource
{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
// TODO: Link to your data model
return 20
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
// TODO: Link to your data model
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath)
return cell
}
func collectionView(_ collectionView: UICollectionView,
moveItemAt sourceIndexPath: IndexPath,
to destinationIndexPath: IndexPath)
{
// TODO: Update your data model to reflect the change
}
}
// MARK: - UICollectionViewDelegate
extension ViewController: UICollectionViewDelegate
{
// TODO: Add any UICollectionViewDelegate here if needed.
}
请注意,此代码不考虑触摸位置偏移-因此当您开始拖动时,您的单元格将“跳跃”到手指下方的中心。如果要防止出现这种情况,则需要在UIViewController
中定义CGPoint
属性(在下面的代码中命名为initialGestureLocationInCell
)。然后在初始示例中替换为:
[...]
case .began:
guard let selectedIndexPath = self.uiCollectionView.indexPathForItem(at: gesture.location(in: self.uiCollectionView)) else
{
break
}
let selectedCell = self.uiCollectionView.cellForItem(at: selectedIndexPath)!
let gestureLocationInCell_RelativeToOrigin = gesture.location(in: selectedCell)
let gestureLocationInCell_RelativeToCentre = CGPoint(x: gestureLocationInCell_RelativeToOrigin.x - selectedCell.frame.size.width/2,
y: gestureLocationInCell_RelativeToOrigin.y - selectedCell.frame.size.height/2)
self.initialGestureLocationInCell = gestureLocationInCell_RelativeToCentre
self.uiCollectionView.beginInteractiveMovementForItem(at: selectedIndexPath)
case .changed:
let gestureLocationInCollectionView = gesture.location(in: gesture.view!)
let targetPosition = CGPoint(x: gestureLocationInCollectionView.x - self.initialGestureLocationInCell.x,
y: gestureLocationInCollectionView.y - self.initialGestureLocationInCell.y)
self.uiCollectionView.updateInteractiveMovementTargetPosition(targetPosition)
[...]
UICollectionViewController在嵌入容器视图时不会安装其重新排序手势识别器,因为installsStandardGestureForInteractiveMovement设置为false。目前还不清楚这是故意的还是一个bug 一个解决办法是: 将嵌入式UICollectionViewController的installsStandardGestureForInteractiveMovement在ViewDidDisplay(或生命周期中的更高版本)中设置为true:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.installsStandardGestureForInteractiveMovement = true
}
重新排序与非嵌入式UICollectionViewController的工作方式相同。数据源只需要声明
func collectionView(\collectionView:UICollectionView,moveItemAt sourceindepath:indepath,to destinationindepath:indepath)
请注意,您需要将collectionView的clipsToBounds设置为false,以便在拖动到集合视图外部时能够看到单元格。但是,这意味着滚动到边界外的单元格也将可见,这可能不可行,具体取决于您的设计。更新了@Scooter第二部分的解决方案示例,非常有用!谢谢。非常感谢您关于触摸位置偏移的建议。这很好,但是有什么方法可以加快手势识别器的速度吗?我只是觉得时间太长了一点。谢谢