I have a collectionView that I can scale on compression.
It works as follows:
I added UIPinchGestureRecognizer to collectionView, when a pinch occurs, I have an invalid layout that forces collectionView to request a delegate for a new size.
It works well.
The problem that I cannot fix is ββthat during the pinch I want to keep my cell in the same position. Just below the indicator in the middle of the screen. (see screenshot).
I thought of storing the current scrollView offset when the pinch starts, when the cell is redrawn with a new size, I calculate the difference in width and add or subtract for contentOffset.
I have a contentInset to scroll the first cell in the middle in the View collection.
Here is my code:
@objc func handlePinchGesture(gesture: UIPinchGestureRecognizer) {
if (gesture.state == .Began) {
scaleStart = metrics.scale
widthStart = collectionView.visibleCells()\[0\].bounds.width
originalContentOffset = collectionView.contentOffset.x
}
else if (gesture.state == .Changed) {
let newScale = metrics.normalizeScale(scaleStart * gesture.scale)
metrics.scale = newScale
collectionView.collectionViewLayout.invalidateLayout()
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)
}
}
It just doesn't work ...
Do you have an idea?
Thanks so much for your input.
Here is a screenshot of what I have and I want with the correct offsets. But I canβt find the right way to calculate the content offset after the hard-press.

Thierry
source
share