Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
398 views
in Technique[技术] by (71.8m points)

objective c - NSUndoManager to undo UIImage rotation using rotation gesture

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

@implementation YourViewController {
    CGAffineTransform _originalImageViewTransform;
}

Next, we need a method that pushes an undo action and sets the view's transform:

- (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;
}

Your handleRotate: method needs to detect the state of the gesture and take the appropriate action.

- (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;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...