NSUndoManager to undo UIImage rotation using rotation gesture(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 旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:NSUndoManager 使用旋转手势撤消 UIImage 旋转
基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01