Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 4th, 2012  |  syntax: None  |  size: 1.97 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. NSUndoManager to undo UIImage rotation using rotation gesture
  2. - (void)handleRotate:(UIRotationGestureRecognizer *)recognizer
  3. {
  4.     if (recognizer.state == UIGestureRecognizerStateBegan) {
  5.         prevRotation = 0.0;
  6.     }
  7.  
  8.     float thisRotate = recognizer.rotation - prevRotation;
  9.     prevRotation = recognizer.rotation;
  10.     recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, thisRotate);
  11.  
  12.     CGPoint lastpoint = point;
  13. }
  14.        
  15. @implementation YourViewController {
  16.     CGAffineTransform _originalImageViewTransform;
  17. }
  18.        
  19. - (void)setTransform:(CGAffineTransform)newTransform ofView:(UIView *)view
  20.     undoTransform:(CGAffineTransform)undoTransform
  21. {
  22.     // If I'm called because the gesture ended, this pushes an undo action.
  23.     // If I'm called because the user requested an undo, this pushes a redo action.
  24.     [[self.undoManager prepareWithInvocationTarget:self]
  25.         setTransform:undoTransform ofView:view undoTransform:newTransform];
  26.  
  27.     // Now actually set the transform.
  28.     view.transform = newTransform;
  29. }
  30.        
  31. - (void)handleRotate:(UIRotationGestureRecognizer *)recognizer {
  32.     UIView *view = recognizer.view;
  33.     UIGestureRecognizerState state = recognizer.state;
  34.  
  35.     if (state == UIGestureRecognizerStateCancelled) {
  36.         view.transform = _originalImageViewTransform;
  37.         return;
  38.     }
  39.  
  40.     if (state == UIGestureRecognizerStateBegan) {
  41.         _originalImageViewTransform = view.transform;
  42.     }
  43.  
  44.     CGAffineTransform transform = view.transform;
  45.     transform = CGAffineTransformRotate(transform, recognizer.rotation);
  46.     recognizer.rotation = 0; // This line means we don't need prevRotation
  47.  
  48.     if (state == UIGestureRecognizerStateEnded) {
  49.         [[ The gesture ended, so push an undo action before setting the transform.
  50.         [self setTransform:transform ofView:view undoTransform:_originalImageViewTransform];
  51.     } else {
  52.         // The gesture changed but didn't end, so don't push an undo action.
  53.         view.transform = transform;
  54.     }
  55. }