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

NSUndoManager to undo UIImage rotation using rotation gesture(NSUndoManager 使用旋转手势撤消 UIImage 旋转)
本文介绍了NSUndoManager 使用旋转手势撤消 UIImage 旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

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

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

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

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

这篇关于NSUndoManager 使用旋转手势撤消 UIImage 旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)