首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在拖动时防止UICollectionViewCell中心移动

在拖动时防止UICollectionViewCell中心移动
EN

Stack Overflow用户
提问于 2016-10-18 19:15:52
回答 2查看 1.5K关注 0票数 3

通过使用手势识别器拖动iOS 9并简化对重新排序的新iOS 9支持,我能够在iOS 9上重新排序。

代码语言:javascript
复制
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()

我注意到当我开始拖动一个细胞时,它的中心变成了触控位置,我不喜欢这样。

如果我想的话,我想把我的手机拖到它的角落。

你知道怎么做吗?

非常感谢。

EN

回答 2

Stack Overflow用户

发布于 2017-08-09 20:33:49

(用Swift 3.1编写)

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

代码语言:javascript
复制
var location = recognizer.location(in: collectionView)            
collectionView.updateInteractiveMovementTargetPosition(location)

..。我创建了一个函数,该函数以要拖动的单元格为中心( collectionView updateInteractiveMovementTargetPosition 使用的位置),然后获取手势识别器的触摸在单元格中的位置,并将其从单元格的中心减去。

代码语言:javascript
复制
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,它将存储偏移量,所以当手势识别器的位置发生变化时,不需要每次调用上面的函数。

所以我手势识别器的目标是这样的:

代码语言:javascript
复制
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()
    }
}
票数 9
EN

Stack Overflow用户

发布于 2018-05-14 04:55:13

如果您的集合视图只朝一个方向滚动,那么实现这一点的最简单的方法就是简单地锁定没有滚动到硬编码的轴,这意味着您的单元格将只在可以滚动的轴上移动。这是代码,请参阅changed情况..。

代码语言:javascript
复制
@objc func handleLongGesture(gesture: UILongPressGestureRecognizer) {
    switch gesture.state {
    case .began:
        guard let selectedIndexPath = self.collectionView
            .indexPathForItem(at: gesture
                .location(in: self.collectionView)) else { break }
        collectionView.beginInteractiveMovementForItem(at: selectedIndexPath)
    case .changed:
        var gesturePosition = gesture.location(in: gesture.view!)
        gesturePosition.x = (self.collectionView.frame.width / 2) - 20
        collectionView.updateInteractiveMovementTargetPosition(gesturePosition)
    case .ended:
        collectionView.endInteractiveMovement()
    default:
        collectionView.cancelInteractiveMovement()
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40116282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档