在捏缩放后保持collectionView单元格位置

时间:2016-06-03 19:52:18

标签: ios uicollectionview

我有一个collectionView,我可以在收缩时进行缩放。

它的工作原理如下:

我在collectionView上添加了一个UIPinchGestureRecognizer,当发生捏合时,我使布局失效,迫使collectionView向代表询问新的大小。

效果很好。

我无法解决的问题是,在捏时我想让我的手机保持在同一位置。就在屏幕中间的指示器下方。 (见截图)。

我想在pinch开始时存储当前的scrollView偏移,然后当使用新的大小重新显示单元格时,我计算宽度差异并添加或减去contentOffset。

我有一个contentInset,以便在collectionView的中间滚动第一个单元格。

这是我的代码:

@objc func handlePinchGesture(gesture: UIPinchGestureRecognizer) {

if (gesture.state == .Began) {
    scaleStart = metrics.scale // remember current scale
    widthStart = collectionView.visibleCells()\[0\].bounds.width // get size of a cell to calulate a difference when scale will change
    originalContentOffset = collectionView.contentOffset.x // remember original content offset
}
else if (gesture.state == .Changed) {

    let newScale = metrics.normalizeScale(scaleStart * gesture.scale) // normalize scale. give 0.5, 1, 1.5, 2

    metrics.scale = newScale // global struct

    //let ZoomIn = (newScale > scaleStart)

    collectionView.collectionViewLayout.invalidateLayout() // invalidate layout in order to redisplay cell with updated scale

    let scaleRatio = newScale / self.scaleStart
    var newContentOffset = CGFloat(0)

    let widthDiff: CGFloat = (scaleRatio * self.widthStart) - self.widthStart

    newContentOffset = originalContentOffset + widthDiff

    self.collectionView.setContentOffset(CGPointMake(newContentOffset ,0), animated: false)
    }
}

它只是不起作用......

你有什么想法吗?

非常感谢您的投入。

这是我拥有和想要的正确偏移的屏幕截图。但是我无法找到一种在捏合手势后计算内容偏移的正确方法。

screenshot

亨利

1 个答案:

答案 0 :(得分:1)

关于最初的问题,您希望缩放中心位于屏幕中间,请尝试以下操作:

@objc func handlePinchGesture(_ gesture: UIPinchGestureRecognizer) {
    let scaleValue: CGFloat = gesture.scale
    if (gesture.state == .began) {
        scaleStart = metrics.scale // remember current scale
        widthStart = collectionView.visibleCells[0].bounds.width // get size of a cell to calulate a difference when scale will change
        originalContentOffset = collectionView.contentOffset.x // remember original content offset
        originalNumberOfCellsToOffset =  (originalContentOffset + (self.view.frame.size.width/2)) / (widthStart * 2)  //for zooming at the middle of the screen
    }
    else if (gesture.state == .changed) {

        let newScale = scaleStart * gesture.scale
        //let newScale = metrics.normalizeScale(scaleStart * gesture.scale) // use this line instead, if you want to normalize scale. give 0.5, 1, 1.5, 2

        metrics.scale = newScale // global struct

        //let ZoomIn = (newScale > scaleStart)

        collectionView.collectionViewLayout.invalidateLayout() // invalidate layout in order to redisplay cell with updated scale

        let scaleRatio = newScale / scaleStart
        var newContentOffset = CGFloat(0)

        let offsetDiffForEachCell: CGFloat = (scaleRatio * widthStart) - widthStart

        newContentOffset = (offsetDiffForEachCell)*originalNumberOfCellsToOffset + (originalContentOffset)

        collectionView.setContentOffset(CGPoint(x: newContentOffset ,y: 0), animated: false)
    }
}

取决于屏幕上collectionView的位置和大小,它可能并不是真正在屏幕中间缩放。如果是这样,您要做的就是更改此行:

originalNumberOfCellsToOffset =  (originalContentOffset + (self.view.frame.size.width/2)) / (widthStart * 2)  //for zooming at the middle of the screen

到例如

originalNumberOfCellsToOffset =  (originalContentOffset + (self.view.frame.size.width/2) - 20) / (widthStart * 2)  //for zooming at the middle of the screen

如果您希望缩放中心向左增加20px。


奖金:如果相反,您想放大捏的中央,然后更改此行:

originalNumberOfCellsToOffset =  (originalContentOffset + (self.view.frame.size.width/2)) / (widthStart * 2)  //for zooming at the middle of the screen

originalNumberOfCellsToOffset = (gesture.location(ofTouch: 0, in: sectionSequenceCollectionView).x + gesture.location(ofTouch: 1, in: sectionSequenceCollectionView).x) / (widthStart * 2)