NSUndoManager 使用旋转手势撤消 UIImage 旋转

发布于 2024-12-29 09:27:27 字数 501 浏览 1 评论 0原文

如何使用 NSUndoManager 使用旋转手势来旋转 UIImageView?这是我的旋转代码。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

转身泪倾城 2025-01-05 09:27:27

首先,阅读 “在 iPhone 上使用撤消”。确保您已在响应程序链中的某个位置(可能在视图控制器中)设置了 undoManager 属性。

我们只想在手势结束时推送撤消操作。但是当我们推动撤消操作时,我们需要知道手势开始时视图的变换。创建一个实例变量来保存原始变换:

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

您的 handleRotate: 方法需要检测手势的状态并采取适当的操作行动。

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

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文