[翻译] TransitionKit

TransitionKit

 

https://github.com/blakewatters/TransitionKit

 

 

 

A simple, elegantly designed block based API for implementing State Machines in Objective-C

一套简单,优雅的基于block设计的API,用以实现OC中的状态机.

 

TransitionKit is a small Cocoa library that provides an API for implementing a state machine in Objective C. It is full-featured, completely documented, and very thoroughly unit tested. State machines are a great way to manage complexity in your application and TransitionKit provides you with an elegant API for implementing state machines in your next iOS or Mac OS X application.

TransitionKit是一个简单的Cocoa库,提供API用以实现OC的状态机.它有着完整的特性,全面的文档以及经过了详细的单元测试.状态机是一个用以解决复杂的UI与数据源业务交互逻辑的一种方案,也许就会用在你的下一款为iOS或者OS X设计的项目当中.

 

Features

  • Supports an arbitrary number of States and Events. 支持任意数目的状态以及事件
  • States and Events support a thorough set of block based callbacks for responding to state transitions. 状态与事件支持一系列基于block的回调,用以响应状态的改变
  • States, Events, and State Machines are NSCopying and NSCoding compliant, enabling easy integration with archiving and copying in your custom classes. 状态,事件以及状态机遵循NSCopying与NSCoding协议,即使你的类中需要支持NSCopying,NSCoding,你也可以方便的使用状态机
  • Strongly enforced. The state machine includes numerous runtime checks for misconfigurations, making it easy to debug and trust your state machines. 状态机非常可靠,可以方便你debug,放行使用
  • Transitions support the inclusion of arbitrary user data via a userInfo dictionary, making it easy to broadcast metadata across callbacks. Transitions支持你传递字典,你可以方便的将元数据进行广播,通过回调的方式
  • Completely Documented. The entire library is marked up with Appledoc. 有着完整的文档,方便你查找
  • Thorougly unit tested. You know it works and can make changes with confidence. 通过了单元测试,请相信,这个状态机是非常可靠的
  • Lightweight. TransitionKit has no dependencies beyond the Foundation library and works on iOS and Mac OS X. 轻量级,TransitionKit不依赖于Foundation框架,你可以便利的在iOS于Mac OS X中使用

 

Installation via CocoaPods

The recommended approach for installing TransitionKit is via the CocoaPods package manager, as it provides flexible dependency management and dead simple installation. For best results, it is recommended that you install via CocoaPods >= 0.16.0 using Git >= 1.8.0 installed via Homebrew.

推荐的安装方式是通过CocoaPods包来安装,它提供了灵活的依赖关系,并让安装变得非常简易.建议你通过Homebrew,使用CocoaPods >= 0.16.0或者是Git >= 1.8.0来安装.

Install CocoaPods if not already available:

安装CocoaPods,如果你还没用过CocoaPods

$ [sudo] gem install cocoapods
$ pod setup

Change to the directory of your Xcode project, and Create and Edit your Podfile and add TransitionKit:

进入你的Xcode工程目录,创建,编辑你的Podfile,然后添加TransitionKit:

$ cd /path/to/MyProject
$ touch Podfile
$ edit Podfile
platform :ios, '5.0'
# Or platform :osx, '10.7'
pod 'TransitionKit', '~> 2.0.0'

Install into your project:

安装到你的项目当中:

$ pod install

Open your project in Xcode from the .xcworkspace file (not the usual project file)

然后,打开.xcworkspace文件即可

$ open MyProject.xcworkspace

 

Examples

The following example is a simple state machine that models the state of a Message in an Inbox.

以下示例是一个简单的使用示例,用以反映状态机的基本运作.

TKStateMachine *inboxStateMachine = [TKStateMachine new];

TKState *unread = [TKState stateWithName:@"Unread"];
[unread setDidEnterStateBlock:^(TKState *state, TKTransition *transition) {
    [self incrementUnreadCount];
}];
TKState *read = [TKState stateWithName:@"Read"];
[read setDidExitStateBlock:^(TKState *state, TKTransition *transition) {
    [self decrementUnreadCount];
}];
TKState *deleted = [TKState stateWithName:@"Deleted"];
[deleted setDidEnterStateBlock:^(TKState *state, TKTransition *transition) {
    [self moveMessageToTrash];
}];

[inboxStateMachine addStates:@[ unread, read, deleted ]];
inboxStateMachine.initialState = unread;

TKEvent *viewMessage = [TKEvent eventWithName:@"View Message" transitioningFromStates:@[ unread ] toState:read];
TKEvent *deleteMessage = [TKEvent eventWithName:@"Delete Message" transitioningFromStates:@[ read, unread ] toState:deleted];
TKEvent *markAsUnread = [TKEvent eventWithName:@"Mark as Unread" transitioningFromStates:@[ read, deleted ] toState:unread];

[inboxStateMachine addEvents:@[ viewMessage, deleteMessage, markAsUnread ]];

// Activate the state machine
[inboxStateMachine activate];

[inboxStateMachine isInState:@"Unread"]; // YES, the initial state

// Fire some events
NSDictionary *userInfo = nil;
NSError *error = nil;
BOOL success = [inboxStateMachine fireEvent:@"View Message" userInfo:userInfo error:&error]; // YES
success = [inboxStateMachine fireEvent:@"Delete Message" userInfo:userInfo error:&error]; // YES
success = [inboxStateMachine fireEvent:@"Mark as Unread" userInfo:userInfo error:&error]; // YES

success = [inboxStateMachine canFireEvent:@"Mark as Unread"]; // NO

// Error. Cannot mark an Unread message as Unread
success = [inboxStateMachine fireEvent:@"Mark as Unread" userInfo:nil error:&error]; // NO

// error is an TKInvalidTransitionError with a descriptive error message and failure reason

 

Unit Tests

TransitionKit is tested using the Kiwi BDD library. In order to run the tests, you must do the following:

TransitionKit使用了Kiwi BDD库进行测试.为了运行这个测试,你需要做如下几步:

  1. Install the dependencies via CocoaPods: pod install 先通过CocoaPods来安装依赖
  2. Open the workspace: open TransitionKit.xcworkspace 然后打开TransitionKit.xcworkspace
  3. Run the specs via the Product menu > Test 然后选择Product menu > Test 来运行

 

Contact

Blake Watters

 

License

TransitionKit is available under the Apache 2 License. See the LICENSE file for more info.

TransitionKit支持Apache 2 协议,请查看Apache 2 协议了解更多.

 

时间: 2024-10-03 10:15:41

[翻译] TransitionKit的相关文章

[翻译]JDK 8 兼容性指南

翻译官方文档,删除部分可忽略. 译者:坤谷,井桐,激酶 兼容性是一个复杂的问题. 本文介绍了Java平台潜在的三种不兼容问题: 源码: 源码兼容性问题关注Java源代码转换成class文件是否兼容,包括代码是否仍然可编译. 二进制: 在Java语言规范中,二进制兼容性定义为:"类的改变是二进制兼容的(或者不破坏二进制兼容性),是指如果改变前的类的二进制在链接时没有错误,那么改变后的类在链接时仍然没有错误." 行为 : 行为兼容性包括在运行时执行的代码的语义. 欲了解更多信息,请参阅Op

java.security.Guard翻译

  Overview Package  Class Use Tree Deprecated Index Help JavaTM 2 PlatformStd. Ed. v1.4.2  PREV CLASS   NEXT CLASSFRAMES    NO FRAMES     All Classes SUMMARY: NESTED | FIELD | CONSTR | METHODDETAIL: FIELD | CONSTR | METHOD java.security Interface Gua

翻译CFSSL相关操作文档

我发现国内这个CFSSL资料蛮少的. 但如果深入到K8S认证之后,这块知识又必不可少. (OPENSSL也可实现,但好像不是主流) 于是花了两天快速翻译了一下几个文档. 贡献给有需要的同仁. 如有错误(肯定有!),欢迎提正. 百度网盘共享地址: https://pan.baidu.com/s/1skAQtAH

如何这段C#代码翻译成VB代码?谢谢!

问题描述 如何这段C#代码翻译成VB代码?谢谢! private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { //自动点击弹出确认或弹出提示 IHTMLDocument2 vDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument; vDocument.parentWindow.execScrip

php-java和PHP有纯中文的官方手册吗?还是只有部分的翻译?

问题描述 java和PHP有纯中文的官方手册吗?还是只有部分的翻译? java和PHP有纯中文的官方手册吗?还是只有部分的翻译?我英文不好,能不能学呢? 解决方案 http://cn2.php.net/manual/zh/index.php 解决方案二: java的JDK有中文版的API,PHP网上也有中文手册.现在网络资源这么丰富,只有肯上心,学东西还是很容易的. 选定一个完整的视频教程,跟着练习,上手是很容易的. 我就是2010年暑假跟着传智播客视频教程自学的Java,现在已经很熟练了.祝好

傲游浏览器怎么翻译网页

  步骤一:没有傲游浏览器的童鞋 步骤二:点开下拉====勾选"翻译". 步骤三:输入你想要的翻译的网址,选定你想翻译的内容 步骤四:耐心等待翻译结果

Word 2010中的“翻译字典”

  1.打开一篇文字文档,并且里面与你有需要翻译的文字,例如,我们这里先选择一篇中文散文; 2.在功能区点击"审阅"选项卡,选择"语言"区域的"翻译"选项组,单击"翻译所选文字"; 3.单击"审阅"功能卡,点击"语言"区域的"翻译"选项组,在弹出的下拉菜单中选择"选择转换语言"; 4.此时窗口会弹出一个"翻译语言选项"的对话框,

Word每一页的左边显示英语右边显示中文翻译

  现在,您想实现的效果是,在每一页里面,左边是英文,右边是对应的翻译好的中文.像这样的排版方式,可以方便我们更好的学习英语. ①使用分栏的是不科学的 要解决这个问题,很多人第一时间就会想到分栏,想把英文放在栏的左边,中文放在栏的右边.然而,这是可行的,却是不科学的. 因为,使用Word里面的自动分栏,原文英文和翻译后的中文,很难一一对应,造成学习上的困难.另外,如果您想再排版实现一一对应,那么,难度是非常大. ②使用文本框也是不合理的 以上方法难以实现.很多人会想到使用文本框的办法. 即在Wo

360浏览器如何翻译英文网页

  整个网页翻译: 1.打开360浏览器,在浏览器中打开你想浏览的英文网站,选择浏览器上面的翻译三角符号. 2.选择第一项,翻译当前网页,就会开始翻译了. 3.翻译完后,就可以看到全部是中文了. 单个句子翻译: 1.同样选择浏览器上面的翻译,选择第二项翻译文字. 2.在弹出的翻译对话框中,将要翻译的英文复制到文本框中,点击下面的翻译. 3.翻译完后,翻译的结果就在下面了,很方便的.