iOS开发UIResponder之NSUndoManager
1、简介
UIResponder有个属性:NSUndoManager
@property(nullable, nonatomic,readonly) NSUndoManager *undoManager NS_AVAILABLE_IOS(3_0);
NSUndoManager可以叫做撤销管理器,可以撤销和重做,类似快捷键command+z和command+shift+z;
NSUndoManger内部维护两个栈,undo栈(撤销)和redo栈(重写)。
2、NSUndoManger的简单使用
2.1、数组的undo和redo
#import "UndoTest.h" @interface UndoTest()
@end
@implementation UndoTest - (instancetype)init{
if (self = [super init]) {
self.undoManager = [[NSUndoManager alloc] init];
self.undoArr = [NSMutableArray array];
}
return self;
}
- (void)addObjectMethod:(NSString *)anObject{
[[self.undoManager prepareWithInvocationTarget:self] removeObjectMehtod:anObject];
[self.undoArr addObject:anObject];
}
- (void)removeObjectMehtod:(NSString *)anObject{
[[self.undoManager prepareWithInvocationTarget:self] addObjectMethod:anObject];
if ([self.undoArr containsObject:anObject]) {
[self.undoArr removeObject:anObject];
}
} @end
UndoTest代码
2.2、视图移动的undo和redo
- (void)viewDidLoad {
[super viewDidLoad];
_undoManager = [[NSUndoManager alloc] init];
}
- (void)btnViewAdd{
[[_undoManager prepareWithInvocationTarget:self] btnViewReduce];
CGRect rect = self.btnView.frame;
rect.origin.x +=;
self.btnView.frame = rect;
}
- (void)btnViewReduce{
[[_undoManager prepareWithInvocationTarget:self] btnViewAdd];
CGRect rect = self.btnView.frame;
rect.origin.x -=;
self.btnView.frame = rect;
}
- (IBAction)addClick:(id)sender {
[self btnViewAdd];
}
- (IBAction)reduceClick:(id)sender {
[self btnViewReduce];
} - (IBAction)undoClick:(UIButton *)sender {
[_undoManager undo];
}
- (IBAction)redoClick:(UIButton *)sender {
[_undoManager redo];
}
视图移动代码
3、NSUndoManger的相关API
NS_CLASS_AVAILABLE(10_0, 3_0)
@interface NSUndoManager : NSObject {
@private
id _undoStack;
id _redoStack;
NSArray *_runLoopModes;
uint64_t _NSUndoManagerPrivate1;
id _target;
id _proxy;
void *_NSUndoManagerPrivate2;
void *_NSUndoManagerPrivate3;
} //创建撤销组
- (void)beginUndoGrouping; //开始
- (void)endUndoGrouping; //结束
@property BOOL groupsByEvent; //默认启用,自动分组,一个RunLoop事件中注册的所有undomanager为一个顶级组
@property (readonly) NSInteger groupingLevel;//组等级 //启用和禁用撤消
- (void)disableUndoRegistration;
- (void)enableUndoRegistration;
@property (readonly, getter=isUndoRegistrationEnabled) BOOL undoRegistrationEnabled; /* Groups By Event */ //限制撤销堆栈
@property NSUInteger levelsOfUndo; //在运行循环的周期内处理输入类型的模式。
@property (copy) NSArray<NSRunLoopMode> *runLoopModes; //执行撤消和重做
- (void)undo;
- (void)redo;
- (void)undoNestedGroup; //检查撤销能力
@property (readonly) BOOL canUndo;
@property (readonly) BOOL canRedo; //检查是否正在执行撤消或重做
@property (readonly, getter=isUndoing) BOOL undoing;
@property (readonly, getter=isRedoing) BOOL redoing; //移除撤销操作
- (void)removeAllActions;
- (void)removeAllActionsWithTarget:(id)target; //注册撤消操作
- (void)registerUndoWithTarget:(id)target selector:(SEL)selector object:(nullable id)anObject;
- (id)prepareWithInvocationTarget:(id)target;
- (void)registerUndoWithTarget:(id)target handler:(void (^)(id target))undoHandler API_AVAILABLE(macos(10.11), ios(9.0), watchos(2.0), tvos(9.0)) NS_REFINED_FOR_SWIFT; //如果撤销组作为一个整体被丢弃,则该密钥具有对应的true值。
FOUNDATION_EXPORT NSString * const NSUndoManagerGroupIsDiscardableKey API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
// used with NSRunLoop's performSelector:target:argument:order:modes:
static const NSUInteger NSUndoCloseGroupingRunLoopOrdering = ; //可撤销的撤消和重做操作
@property (readonly) BOOL undoActionIsDiscardable API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
@property (readonly) BOOL redoActionIsDiscardable API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0));
- (void)setActionIsDiscardable:(BOOL)discardable API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0)); //管理操作名字
@property (readonly, copy) NSString *undoActionName;
@property (readonly, copy) NSString *redoActionName;
- (void)setActionName:(NSString *)actionName; //获取和本地化菜单项标题
@property (readonly, copy) NSString *undoMenuItemTitle;
@property (readonly, copy) NSString *redoMenuItemTitle;
- (NSString *)undoMenuTitleForUndoActionName:(NSString *)actionName;
- (NSString *)redoMenuTitleForUndoActionName:(NSString *)actionName; @end
//通知
//打开或关闭撤销组
FOUNDATION_EXPORT NSNotificationName const NSUndoManagerCheckpointNotification API_AVAILABLE(macos(10.0), ios(3.0), watchos(2.0), tvos(9.0));
//将要执行一个撤销操作
FOUNDATION_EXPORT NSNotificationName const NSUndoManagerWillUndoChangeNotification API_AVAILABLE(macos(10.0), ios(3.0), watchos(2.0), tvos(9.0));
//将要执行重做操作
FOUNDATION_EXPORT NSNotificationName const NSUndoManagerWillRedoChangeNotification API_AVAILABLE(macos(10.0), ios(3.0), watchos(2.0), tvos(9.0));
//已经执行一个撤销操作
FOUNDATION_EXPORT NSNotificationName const NSUndoManagerDidUndoChangeNotification API_AVAILABLE(macos(10.0), ios(3.0), watchos(2.0), tvos(9.0));
//已经执行重做操作
FOUNDATION_EXPORT NSNotificationName const NSUndoManagerDidRedoChangeNotification API_AVAILABLE(macos(10.0), ios(3.0), watchos(2.0), tvos(9.0)); //已经打开一个撤销组时 beginUndoGrouping
FOUNDATION_EXPORT NSNotificationName const NSUndoManagerDidOpenUndoGroupNotification API_AVAILABLE(macos(10.0), ios(3.0), watchos(2.0), tvos(9.0));
//将要关闭一个撤销组时 endUndoGrouping
FOUNDATION_EXPORT NSNotificationName const NSUndoManagerWillCloseUndoGroupNotification API_AVAILABLE(macos(10.0), ios(3.0), watchos(2.0), tvos(9.0));
//已经关闭一个撤销组时 endUndoGrouping
FOUNDATION_EXPORT NSNotificationName const NSUndoManagerDidCloseUndoGroupNotification API_AVAILABLE(macos(10.7), ios(5.0), watchos(2.0), tvos(9.0)); NS_ASSUME_NONNULL_END
iOS开发UIResponder之NSUndoManager的更多相关文章
- iOS开发UIResponder简介API
#import <Foundation/Foundation.h> #import <UIKit/UIKitDefines.h> #import <UIKit/UIEve ...
- 【IOS开发笔记02】学生管理系统
端到端的机会 虽然现在身处大公司,但是因为是内部创业团队,产品.native.前端.服务器端全部坐在一起开发,大家很容易做零距离交流,也因为最近内部有一个前端要转岗过来,于是手里的前端任务好像可以抛一 ...
- ios开发中的小技巧
在这里总结一些iOS开发中的小技巧,能大大方便我们的开发,持续更新. UITableView的Group样式下顶部空白处理 //分组列表头部空白处理 UIView *view = [[UIViewal ...
- iOS开发系列--IOS程序开发概览
概览 终于到了真正接触IOS应用程序的时刻了,之前我们花了很多时间去讨论C语言.ObjC等知识,对于很多朋友而言开发IOS第一天就想直接看到成果,看到可以运行的IOS程序.但是这里我想强调一下,前面的 ...
- iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控
-- iOS事件全面解析 概览 iPhone的成功很大一部分得益于它多点触摸的强大功能,乔布斯让人们认识到手机其实是可以不用按键和手写笔直接操作的,这不愧为一项伟大的设计.今天我们就针对iOS的触摸事 ...
- iOS开发-UI 从入门到精通(三)
iOS开发-UI 从入门到精通(三)是对 iOS开发-UI 从入门到精通(一)知识点的综合练习,搭建一个简单地登陆界面,增强实战经验,为以后做开发打下坚实的基础! ※在这里我们还要强调一下,开发环境和 ...
- iOS开发——UI精选OC篇&UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍
UIApplication,UIWindow,UIViewController,UIView(layer)简单介绍 一:UIApplication:单例(关于单例后面的文章中会详细介绍,你现在只要知道 ...
- IOS开发常用设计模式
IOS开发常用设计模式 说起设计模式,感觉自己把握不了笔头,所以单拿出iOS开发中的几种常用设计模式谈一下. 单例模式(Singleton) 概念:整个应用或系统只能有该类的一个实例 在iOS开发我们 ...
- 解析iOS开发中的FirstResponder第一响应对象
1. UIResonder 对于C#里所有的控件(例如TextBox),都继承于Control类.而Control类的继承关系如下: 代码如下: System.Object System.Marsha ...
随机推荐
- 使用springBoot和mybatis整合时出现如下错误:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)解决方案
在pom.xml文件中添加如下: <build> <resources> <resource> & ...
- BZOJ 3159: 决战 解题报告
BZOJ 3159: 决战 1 sec 512MB 题意: 给你一颗\(n\)个点,初始点权为\(0\)的有跟树,要求支持 Increase x y w 将路径\(x\)到\(y\)所有点点权加上\( ...
- 高级运维(三):部署Lnmp环境、构建Lnmp平台、地址重写
一.部署LNMP环境 目标: 安装部署Nginx.MariaDB.PHP环境 1> 安装部署Nginx.MariaDB.PHP.PHP-FPM: 2> 启动Nginx.MariaDB.FP ...
- jmeter登录之-动态参数
jmeter登录之-动态参数 1.抓包查看提交的登录参数 发现参数authenticity_token是动态的,每次都不一样,所以回放的时候就会失败 2.提取动态变化的参数-后置处理器(相当于LR的关 ...
- PHP fpassthru() 函数
定义和用法 fpassthru() 函数输出文件指针处的所有剩余数据. 该函数将给定的文件指针从当前的位置读取到 EOF,并把结果写到输出缓冲区. 语法 fpassthru(file) 参数 描述 f ...
- [SCOI2014]方伯伯的玉米田 题解(树状数组优化dp)
Description 方伯伯在自己的农田边散步,他突然发现田里的一排玉米非常的不美. 这排玉米一共有N株,它们的高度参差不齐. 方伯伯认为单调不下降序列很美,所以他决定先把一些玉米拔高,再把破坏美感 ...
- 12.RabbitMQ多机集群
配置两台Linux CentOS 6.7虚拟主机 CentOS6.7下载地址 https://pan.baidu.com/s/1i5GPg9n 安装视频下载 https://pan.baidu.c ...
- 网络错误修复工具:Network Fault Repair Tool Build20160414
::请勿轻易修改此文件,以避免不可预知的错误 gwsbhqt@163.com @echo off color 0A setlocal enabledelayedexpansion title Netw ...
- maven学习整理-基础知识
1.maven认识 maven是一种自动化的构建工具,它主要解决的问题有: ①项目中的划分规则:原先我们用package或文件夹的形式来划分不同模块,导致在一个项目中存在大量的文件夹和包代码显得庞大: ...
- 将.opt、.frm、.MYD、.MYI文件放入mysql
问题:如果数据库没有给sql脚本而且给的.opt..frm..MYD..MYI这些文件,应该如何加载呢???? 解答:首先需要找到“mysql的安装目录/data/”,怎么找?mysql命令执行“sh ...