Espresso 3.0.3

Espresso 3.0.3

Mitch Treece 维护。



Espresso 3.0.3

Espresso

Espresso

iOS Swift Cocoapods License

Espresso是一个Swift便利库,用于iOS。
点滴咖啡让一切更美好。☕️

安装

CocoaPods

use_frameworks!
pod 'Espresso', '~> 3.0'

子规格

Espresso被分成几个子规格,这使得选择需要的功能既快速又简单。默认情况下,安装了UIKit子规格。

  • Core:核心类、扩展和依赖项
  • UI
    • UI-Core:通用UI类、扩展和依赖项。
    • UI-UIKit:UIKit类、扩展和依赖项
    • UI-SwiftUI:SwiftUI类、扩展和依赖项
  • PromiseKit:PromiseKit类、扩展和依赖项
  • UIKit:包括所有与UIKit相关的内容的聚合子规格
  • SwiftUI:包括所有与SwiftUI相关的内容的聚合子规格
  • All:包括所有内容的聚合子规格

如果您不确定需要什么样的功能,我们还有一些“配方”子规范可以用作起点,它们集成了Espresso类和一些常见的第三方依赖。

Espresso

Espresso为在苹果平台开发时经常使用的组件添加了大量有用的功能和扩展。

其中一些更有趣的功能包括

  • UIAnimation 类,具有类似Promise的链式系统
  • UIViewControllerTransition 系统,易于自定义 UIViewController 切换
  • AppleDevice 识别与信息
  • MVVM 基类(例如,ViewModelUIViewModelViewUIViewModelViewController
  • Combine 辅助类和扩展
  • 加密和摘要哈希辅助函数
  • 用户身份验证(面部识别,触摸ID,密码)辅助函数
  • 更多功能!

UIAnimation

Espresso包含一个基于 `UIViewPropertyAnimator` 的强大动画系统。通过时间曲线、持续时间、延迟和动画闭包来创建动画。

let view = UIView()
view.alpha = 0

// Simple curve (default timing + default values)

UIAnimation {
    view.alpha = 1
}.start()

// Simple curve (default timing + custom values)

UIAnimation(duration: 0.5, delay: 0) {
    view.alpha = 1
}.start()

// Simple curve (custom)

UIAnimation(.simple(.easeOut), duration: 0.4) {
    view.alpha = 1
}.start()

// Spring curve

UIAnimation(.spring(damping: 0.9, velocity: 0.25)) {
    view.alpha = 1
}.start {
    print("The animation is done!")
}

当前支持以下时间曲线:

  • 简单
  • 贝塞尔
  • 弹簧
  • 默认弹簧
  • 材料
  • 自定义

UIAnimation 还支持动画 链式。这使得您可以轻松地定义一系列连续运行的动画(类似于关键帧动画),使用类似Promise的语法。

UIAnimation(duration: 0.3) {
    view.alpha = 1
}.then {
    view.backgroundColor = .red
}.start()

在链式中,所有常规 UIAnimation 的参数都可以供您使用

UIAnimation(duration: 0.3) {
    view.alpha = 1
}.then(.defaultSpring, duration: 0.4) {
    view.backgroundColor = UIColor.red
}.start()

动画可以稍后创建和执行!还支持直接从数组中运行动画而不使用链式。

let a1 = UIAnimation {
    view.alpha = 1
}

let a2 = UIAnimation(.simple(.easeIn), duration: 0.5) {
    view.backgroundColor = UIColor.red
}

[a1, a2].start {
    print("The animations are done!")
}

UIViewControllerTransition

基于 UIAnimation 构建,Espresso 的视图控制器切换系统使得轻松地将美观的自定义切换添加到您的应用中。一个简单的 UIViewControllerTransition 实现可能看起来像这样:

class CustomFadeTransition: UIViewControllerTransition {

    public override func animations(using ctx: Context) -> UIAnimationGroupController {

        let sourceVC = ctx.sourceViewController
        let destinationVC = ctx.destinationViewController
        let container = ctx.containerView
        let context = ctx.context

        return UIAnimationGroupController(setup: {

            destinationVC.view.alpha = 0
            destinationVC.view.frame = context.finalFrame(for: destinationVC)
            container.addSubview(destinationVC.view)

        }, animations: {

            UIAnimation {
                destinationVC.view.alpha = 1
            }

        }, completion: {

            context.completeTransition(!context.transitionWasCancelled)

        })

    }

}

从过渡子类中只需重写一个函数,即 animations(using:) 函数。此函数为您提供了关于切换的上下文信息,并期望您返回一个包含设置、动画和完成闭包的 UIAnimationGroupController

要在切换中使用来展示视图控制器,请在其展示之前设置它的 `transition` 属性。已在 UIViewControllerUINavigationController 上添加了辅助函数。

let transition = CustomFadeTransition()

present(
    viewController, 
    using: transition
)

navigationController.push(
    viewController, 
    using: transition
)

Espresso 包括以下视图控制器切换:

  • UIFadeTransition
  • UISlideTransition
  • UICoverTransition
  • UIRevealTransition
  • UISwapTransition
  • UIPushBackTransition
  • UIZoomTransition

用户认证

UserAuthenticator.authenticate(withReason: "The app needs to authenticate you.") { (success, error) in
    print("Authenticated: \(success)")
}

摘要哈希

哈希扩展在DataString中都是可用的。

let data = Data()
let hashedData = data.hashed(using: .md5)

let string = "Hello, world!"
let hashedString = string.hashed(using: .md5)

Espresso包含了以下哈希类型:

  • md5
  • sha1
  • sha224
  • sha256
  • sha384
  • sha512

贡献

欢迎提交 pulls requests。修复错误?添加新功能?打开一个 PR,我们将将其合并!🎉