gpt4 book ai didi

uicollectionview - 拖动时防止通过其中心移动 UICollectionViewCell

转载 作者:行者123 更新时间:2023-12-03 23:22:59 27 4
gpt4 key购买 nike

我可以通过使用手势识别器拖动它并简化新的 iOS 9 对重新排序的支持来重新排序 iOS 9 上的 UICollectionViewCells。

public func beginInteractiveMovementForItemAtIndexPath(indexPath: NSIndexPath) -> Bool // returns NO if reordering was prevented from beginning - otherwise YES
public func updateInteractiveMovementTargetPosition(targetPosition: CGPoint)
public func endInteractiveMovement()
public func cancelInteractiveMovement()

我注意到当我开始拖动一个单元格时,它的中心会变成触摸位置,我不喜欢那样。

如果我愿意,我希望能够通过它的角落拖动我的单元格。

你知道怎么做吗?

非常感谢。

最佳答案

(用 Swift 3.1 编写)

对于 targetPosition updateInteractiveMovementTargetPosition 的参数功能,而不是像这样直接使用手势识别器的位置......

var location = recognizer.location(in: collectionView)            
collectionView.updateInteractiveMovementTargetPosition(location)

...我创建了一个函数,该函数以要拖动的单元格为中心(collectionView updateInteractiveMovementTargetPosition 使用的位置,然后获取手势识别器触摸的位置 在单元格 ,然后从单元格的中心减去它。
func offsetOfTouchFrom(recognizer: UIGestureRecognizer, inCell cell: UICollectionViewCell) -> CGPoint {

let locationOfTouchInCell = recognizer.location(in: cell)

let cellCenterX = cell.frame.width / 2
let cellCenterY = cell.frame.height / 2

let cellCenter = CGPoint(x: cellCenterX, y: cellCenterY)

var offSetPoint = CGPoint.zero

offSetPoint.y = cellCenter.y - locationOfTouchInCell.y
offSetPoint.x = cellCenter.x - locationOfTouchInCell.x

return offSetPoint

}

我有一个简单的 var offsetForCollectionViewCellBeingMoved: CGPoint = .zero在我的 View Controller 中,它将存储该偏移量,因此每次手势识别器的位置更改时都不需要调用上面的函数。

所以我的手势识别器的目标看起来像这样:
func collectionViewLongPressGestureRecognizerWasTriggered(recognizer: UILongPressGestureRecognizer) {

guard let indexPath = collectionView.indexPathForItem(at: recognizer.location(in: self.collectionView)),
let cell = collectionView.cellForItem(at: indexPath), indexPath.item != 0 else { return }

switch recognizer.state {

case .began:

collectionView.beginInteractiveMovementForItem(at: indexPath)

// This is the class variable I mentioned above
offsetForCollectionViewCellBeingMoved = offsetOfTouchFrom(recognizer: recognizer, inCell: cell)

// This is the vanilla location of the touch that alone would make the cell's center snap to your touch location
var location = recognizer.location(in: collectionView)

/* These two lines add the offset calculated a couple lines up to
the normal location to make it so you can drag from any part of the
cell and have it stay where your finger is. */

location.x += offsetForCollectionViewCellBeingMoved.x
location.y += offsetForCollectionViewCellBeingMoved.y

collectionView.updateInteractiveMovementTargetPosition(location)

case .changed:

var location = recognizer.location(in: collectionView)

location.x += offsetForCollectionViewCellBeingMoved.x
location.y += offsetForCollectionViewCellBeingMoved.y

collectionView.updateInteractiveMovementTargetPosition(location)

case .ended:
collectionView.endInteractiveMovement()

default:
collectionView.cancelInteractiveMovement()
}
}

关于uicollectionview - 拖动时防止通过其中心移动 UICollectionViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40116282/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com