How to use NSUndoManager
to rotate a UIImageView
using rotation gesture? This is my code for rotation.
- (void)handleRotate:(UIRotationGestureRecognizer *)recognizer
{
if (recognizer.state == UIGestureRecognizerStateBegan) {
prevRotation = 0.0;
}
float thisRotate = recognizer.rotation - prevRotation;
prevRotation = recognizer.rotation;
recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, thisRotate);
CGPoint lastpoint = point;
}
First of all, read “Using Undo on iPhone”. Make sure you have set the
undoManager
property somewhere in your responder chain (probably in your view controller).We only want to push an undo action when the gesture ends. But when we push the undo action, we need to know the view's transform when the gesture started. Create an instance variable to hold that original transform:
Next, we need a method that pushes an undo action and sets the view's transform:
Your
handleRotate:
method needs to detect the state of the gesture and take the appropriate action.