您的位置:首页 > 移动开发 > IOS开发

IT English Collection(26) of Undo manager

2013-10-20 12:21 531 查看
1 前言

  本节主要介绍了撤销和重做的原理,使用方法和主要用处。

  转载请注明出处:http://blog.csdn.net/developer_zhang

2 详述

2.1 原文

  An
undo manager is a framework object for recording changes to object state and later undoing those changesupon user request. The undo manager, which is an instance of the NSUndoManager class,conducts
both undo and redo operations. An undo operationreverts a change to a property of an object; a redo operation restores theoriginal value if an undo operation has taken place. The ability
to perform undo and redo operations is avaluable and differentiating feature of applications,especially those that allow users to change dataextensively.

  You obtain an undo manager through the undoManager property of UIResponder (iOS) or by calling the undoManager method of NSResponder (OS X). Every window of an application provides a default undo manager, although you can
create private ones for specific contexts.

Undo and Redo Operations Send Messages to the Changed Object

  You must register an undo operation with the undo manager. When the undo manager executes the operation, it sends a messagecomposed from severalcomponents supplied in
registration:

  The object to receive the message. The receiving object is typically an object having a property whose value has changed.

  The signature of the method to invoke. The signature often identifies a setter accessor method.

  The argument of the message. The argument is typically the previous value of the property that has changed. For redo operations, the argument is the changed value.

An undo manager collects all undo operations that occur within a single cycle of a run loop such as the application’s main event loop; executing an undo operation reverts all changes that occurred during the cycle. When performing an undo operation, an undo
manager saves the operations that were reverted so that users can redo what has been undone.



Undo and Redo Operations Are Put on Stacks

An undo manager stores undo and redo operations on first-in last-out stacks. When an application first registers an undo operation, the undo manager adds it to the undo stack. The redo stack, however, remains empty until the user requests an undo operation.
When that happens, the undo manager uses the topmost group of operations on the undo stack tocompose and send one or more messages to revert changes.

  Since these operations again cause changes to one or more object’s state, the objects register new operations with the undo manager, this time in thereverse direction from theoriginal operations.
Because the undo manager is currently undoing changes, it records these operations as redo operations on the redo stack.Subsequent redo operations pull the operations off the redo stack, apply them to the objects, and push
them back onto the undo stack. Because a new change to an object’s stateinvalidates the previous change, as soon as a new undo operation is registered, an undo manager clears the redo stack.

Operations Are Coalesced into Undo Groups

  An undo manager collects undo and redo operations in groups that
represent actions that are entirely
revertible. A group typically identifies properties that are related in some way. It stores these groups of operations as items on the undo and redo stacks. Even a single action must be packaged as a group. NSUndoManager normally creates undo groups
automatically during a cycle of the run loop. The first time it is asked to record an undo operation in the cycle, it creates a new group. Then, at the end of the cycle, it closes the group. You can createadditional,nested
undo groups.

How to Register and Request Undo and Redo Operations

  The NSUndoManager class gives you two ways to register undo operations:

For simple registration, call registerUndoWithTarget:selector:object:, which takes a target object, a selector, and a single argument.

For invocation-based registration, call prepareWithInvocationTarget:, passing in a NSInvocation object thatencapsulates the message to send for reverting changes. With thisapproach you can
have any number and type of arguments.

You often call these methods in the accessor method for a property of an object that is being changed. To request an undo or redo operation, send undo or redo message to a NSUndoManager object.

The Undo Manager and the Responder Chain

  The responder chain plays an important role in undo management, especially when more than the default undo manager is in play. The responder class in either the UIKit or AppKit frameworks (UIResponder or NSResponder) declares the undoManager property and
undoManagermethod, respectively. When an application receives an undo event, the responder class starts with the first responder and from there goes up the responder chain looking for an NSUndoManager object (through the
undoManager property or method). If the end of the responder chain is reached, the window object creates and returns the default undo manager.

  If you want an undo manager for a specific context in addition to the default undo manager, your responder subclass can implement the undoManager property or method to create and return one. The responder subclass is often a custom view but it can also be,
on iOS, a view controller (which must ensure that it becomes first responder). In OS X, the delegate of a window object can implement the windowWillReturnUndoManager: to return an undo manager.

The User Interface for Requesting Undo and Redo

  Users request undo and redo operations in OS X and iOS in different ways. In a Mac app, the user chooses an Undo or Redo menu command from the Edit menu. In an iOS application, the user shakes the device. For a shaking motion to function as command, the applicationSupportsShakeToEdit
property of UIApplication must be set to YES and there must be an undo manager in anappropriate state. If these conditions apply, the application displays an alert view with buttons for undo and redo. In both iOS and OS X,
you can give specific action names to undo and redo commands—for example, Undo Color Change.

2.2 生词

undo[ʌn'duː]vt. 取消;解开;破坏;扰乱
upon[ə'pɒn]prep. 根据;接近;在…之上
conduct['kɒndʌkt]vt. 管理;引导;表现
redo[riː'duː]vt. 重做;重新装饰
revert[rɪ'vɜːt]vi. 回复;重提;返祖遗传;归还
original[ə'rɪdʒɪn(ə)l; ɒ-]adj. 原始的;最初的;独创的;新颖的
valuable['væljʊb(ə)l]adj. 有价值的;贵重的;可估价的
especially[ɪ'speʃ(ə)lɪ; e-]adv.特别;尤其;格外
extensively[ik'stensivli]adv.广阔地;广大地
obtain[əb'teɪn]vi. 获得;流行
composed[kəm'pəʊzd]v. 组成;作曲(compose的过去分词);著作
components[kəm'pəunənt]n. 部件;组件;成份(component复数)
registration[redʒɪ'streɪʃ(ə)n]n.登记;注册;挂号
signature['sɪgnətʃə]n. 署名;签名;信号
reverse[rɪ'vɜːs]vt. 颠倒;倒转
original[ə'rɪdʒɪn(ə)l; ɒ-]adj. 原始的;最初的;独创的;新颖的
subsequent['sʌbsɪkw(ə)nt]adj.后来的,随后的
invalidate[ɪn'vælɪdeɪt]vt.使无效;使无价值
coalesce[,kəʊə'les]vi. 合并;结合;联合
represent[reprɪ'zent]vt.代表;表现
entirely[ɪn'taɪəlɪ; en-]adv.完全地,彻底地
revertible[ri'və:təbl]adj. 应归还的;可恢复原状的
additional[ə'dɪʃ(ə)n(ə)l]adj.附加的,额外的
nested['nestɪd]adj. 嵌套的,内装的
encapsulate[ɪn'kæpsjʊleɪt; en-]vt. 压缩;将…装入胶囊;将…封进内部
approach[ə'prəʊtʃ]n. 方法;途径;接近
respectively[rɪ'spektɪvlɪ]adv. 分别地;各自地,独自地
ensure[ɪn'ʃɔː; -'ʃʊə; en-]vt.保证,确保;使安全
appropriate[ə'prəʊprɪət]adj.适当的
3 结语

  以上是所有内容,希望对大家有所帮助。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  undo redo 撤销 重做 IOS