- NSUndoManager to undo UIImage rotation using rotation gesture
- - (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;
- }
- @implementation YourViewController {
- CGAffineTransform _originalImageViewTransform;
- }
- - (void)setTransform:(CGAffineTransform)newTransform ofView:(UIView *)view
- undoTransform:(CGAffineTransform)undoTransform
- {
- // If I'm called because the gesture ended, this pushes an undo action.
- // If I'm called because the user requested an undo, this pushes a redo action.
- [[self.undoManager prepareWithInvocationTarget:self]
- setTransform:undoTransform ofView:view undoTransform:newTransform];
- // Now actually set the transform.
- view.transform = newTransform;
- }
- - (void)handleRotate:(UIRotationGestureRecognizer *)recognizer {
- UIView *view = recognizer.view;
- UIGestureRecognizerState state = recognizer.state;
- if (state == UIGestureRecognizerStateCancelled) {
- view.transform = _originalImageViewTransform;
- return;
- }
- if (state == UIGestureRecognizerStateBegan) {
- _originalImageViewTransform = view.transform;
- }
- CGAffineTransform transform = view.transform;
- transform = CGAffineTransformRotate(transform, recognizer.rotation);
- recognizer.rotation = 0; // This line means we don't need prevRotation
- if (state == UIGestureRecognizerStateEnded) {
- [[ The gesture ended, so push an undo action before setting the transform.
- [self setTransform:transform ofView:view undoTransform:_originalImageViewTransform];
- } else {
- // The gesture changed but didn't end, so don't push an undo action.
- view.transform = transform;
- }
- }