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 旋转
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01