YLStateMachine 1.1.2

YLStateMachine 1.1.2

YuLeiFuYun 维护。



  • 原创
  • YuLeiFuYun

YLStateMachine

需求

  • iOS 13.0+
  • Swift 5.1+

安装

Cocoapods

要使用 CocoaPods 在 Xcode 项目中集成 YLStateMachine,请在 Podfile 中指定它

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '13.0'
use_frameworks!

target 'MyApp' do
  # your other pod
  # ...
  pod 'YLStateMachine'
end

运行 pod install 来构建您的依赖。

Swift 包管理器

选择文件 > Swift 包 > 添加包依赖。在“选择包存储库”对话框中输入 https://github.com/YuLeiFuYun/YLStateMachine.git

使用方法

创建一个“状态”,并遵循StateType协议。

enum RefreshState: StateType {
    case initial
    case refreshing
    case paginated
	...
    
    var stability: Stability {
        switch self {
        case .refreshing:
            return .transitional
        default:
            return .stable
        }
    }
    
    static var initialState: RefreshState {
        return .initial
    }
}

创建一个“操作”,并遵循ActionType协议。

enum RefreshAction: ActionType {
    case pullToRefresh
    case loadingMore
    
    var transitionState: RefreshState {
        switch self {
        ...
        }
    }
}

创建一个“操作符”,并遵循OperatorType协议。

class RefreshOperator: OperatorType {
    typealias Action = RefreshAction
    ...
    
    // Optional
    func startTransition(_ state: Action.State) {
        ...
    }
    
    // Required
    func transition(with action: RefreshAction, completion: @escaping (RefreshState) -> Void) {
        ...
    }
    
    // Optional
    func endTransition(_ state: Action.State) {
        ...
    }
}

创建状态机。

let refreshOperator = RefreshOperator()
let refreshStateMachine = StateMachine(operator: refreshOperator)

最后,使用这个状态机。

refreshStateMachine.trigger(someAction) {
    ...
}

许可证

YLStateMachine遵循MIT许可证发布。详情请参阅LICENSE文件。