What is a good way to reset a UIPanGestureRecognizer's view to its original frame once the pan gesture is done?

Say you have a pinch gesture recognizer and pan gesture recognizer on an image view:

@IBAction func pinchPiece(_ pinchGestureRecognizer: UIPinchGestureRecognizer) {
    guard pinchGestureRecognizer.state == .began || pinchGestureRecognizer.state == .changed,
          let piece = pinchGestureRecognizer.view else {
            // After pinch releases, zoom back out.
            if pinchGestureRecognizer.state == .ended {
              UIView.animate(withDuration: 0.3, animations: {
                pinchGestureRecognizer.view?.transform = CGAffineTransform.identity
              })
            }
            return
    }
    adjustAnchor(for: pinchGestureRecognizer)
    
    let scale = pinchGestureRecognizer.scale
    piece.transform = piece.transform.scaledBy(x: scale, y: scale)
    pinchGestureRecognizer.scale = 1 // Clear scale so that it is the right delta next time.
}

@IBAction func panPiece(_ panGestureRecognizer: UIPanGestureRecognizer) {
    guard panGestureRecognizer.state == .began || panGestureRecognizer.state == .changed,
          let piece = panGestureRecognizer.view else {
        return
    }
    let translation = panGestureRecognizer.translation(in: piece.superview)
    piece.center = CGPoint(x: piece.center.x + translation.x, y: piece.center.y + translation.y)
    panGestureRecognizer.setTranslation(.zero, in: piece.superview)
}

public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
                            shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    true
}

The pinch gesture's view resets to its original state after the gesture is done, which occurs in its else clause. What would be a good way to do the same for the pan gesture recognizer? Ideally I'd like the gesture recognizers to be in an extension of UIImageView, which would also mean that I can't add a store property to the extension for tracking the initial state of the image view.

I see your new post. have you concluded on the 2 finger pan ? If so, don't forget to close this other thread, to avoid leaving them dangling in the forum. If not, could you explain the raining problem ?

Why don't you save the view frame at beginning of pan and use it if state == .ended ?

Yes sorry, just accepted your answer on the other thread, it worked.

You could use associated objects to store the original location if you want this to be in a category on UIImageView. Also you probably want to override any setter that can change the 'original' location of the image view (like setFrame:, setCenter:, setBounds: and setTransform:) and make sure you update the memorized location while pinching.

You can also use the transform property to move the view around, similar you do with the scale of the pinch. The same issues with both approaches remain though, transform could be set by other places in your app or the system. So you'd need to make sure to keep track of what the 'expected' value of the transform is instead of just restoring to the identity transform.

Turns out a simple solution was hiding in plain sight, I just set the gesture recognizer's view frame to the superview frame, since it has a parent scroll view.

What is a good way to reset a UIPanGestureRecognizer's view to its original frame once the pan gesture is done?
 
 
Q