该仓库提供了一种新的声明式动画描述方式
Sequential {
Parallel {
someView.ca.frame.origin.y(100)
someView.ca.backgroundColor(.red).duration(relative: 0.2)
}
Parallel {
someView.ca.transform(CGAffineTransform(rotationAngle: CGFloat.pi / 3))
someView.ca.backgroundColor(.white).duration(0.1)
Sequential {
someView.ca.backgroundColor(.blue)
someView.ca.backgroundColor(.green)
}
}
UIViewAnimate {
self.imageHeightConstraint.constant = 50
self.view.layoutIfNeeded()
}
TimerAnimation { progress in
someLabel.textColor = (UIColor.white...UIColor.red).at(progress)
}
}
.curve(.easeInOut)
.duration(3)
.start()
简单的 UIKit 动画,通过闭包初始化
UIViewAnimate {
...
}
.duration(0.3)
.start()
简单的 SwiftUI 动画,通过闭包初始化
struct ExampleView {
@StateObject var animations = AnimationsStore()
@State private var someValue: Value
var example1: some View {
VStack {
Button(Text("Tap")) {
Sequential {
Animate {
$someValue =~ newValue
}
.duration(0.3)
Animate { progress in
someValue = (from...to).at(progress)
//progress may be 0 or 1
//or any value in 0...1 if animation is interactive
}
.duration(0.3)
}
.store(animations)
.start()
}
}
.with(animations)
}
var example2: some View {
VStack {
Slider(value: $animations.progress, in: 0...1)
Button("Play") {
animations.play()
}
Button("Pause") {
animations.pause()
}
}
.with(animations) {
Animate {
$someValue =~ newValue
}
.duration(2)
}
}
}
顺序动画,一个接一个地运行
Sequential {
Animate { ... }.duration(relative: 0.5)
Interval(0.1)
Parallel { ... }
}
.duration(1)
.start()
并行动画,同时运行
Parallel {
Animate { ... }.duration(relative: 0.5)
Sequential { ... }
}
.duration(1)
.start()
时间间隔
Interval(1)
任何代码块,持续时间始终为零
Instant {
...
}
TimerAnimation { progress in
...
}
方法 .start() 或 .delegate() 返回 AnimationDelegateProtocol 对象
.isRunning: Bool { get }.position: AnimationPosition { get nonmutating set }.options: AnimationOptions { get }.play(with options: AnimationOptions).pause().stop(at position: AnimationPosition?).add(completion: @escaping (Bool) -> Void).cancel().duration(TimeInterval) - 设置动画持续时间,单位为秒.duration(relative: Double) - 设置动画持续时间,相对于父动画的比例,范围为 0...1.curve(BezierCurve) - 设置动画曲线.spring(dampingRatio: CGFloat = 0.3) - 设置弹簧动画曲线 (仅适用于 UIViewAnimate).repeat(), .repeat(Int) - 重复动画.autoreverse(), .autoreverse(repeat: Int) - 自动反转动画.reversed() - 反转动画.ca - UIView、CALayer 和 View、Binding 扩展,用于描述属性动画someView.ca.backgroundColor(.white).layer.cornerRadius(8).tintColor(.red).duration(0.3).start()
VDAnimation 提供了一种简单的方式来描述 UIViewController 转场。VDAnimation 还支持类似于 Keynote 的 Magic Move 或 Hero 的转场。它检查所有源视图和目标视图上的 .transition.id 属性。 然后自动将每个匹配的视图对从旧状态转换为新状态。
viewController.transition.isEnabled = true
viewController.transition.duration = 0.4
viewController.transition.curve = .easeIn
viewController.transition.modifier = .edge(.bottom)
viewController.transition.interactive.disappear = .swipe(to: .bottom)
present(viewController, animated: true)
fromVc.someView.transition.id = "source"
toVc.someView.transition.id = "source"
fromVc.someView2.transition.modifier = .scale.offset(10)
to.someView2.transition.modifier = .scale.offset(-10)
toVc.transition.isEnabled = true
viewController.transition.interactive.disappear = .swipe(to: .bottom)
present(toVc, animated: true)
toVc.transition = .pageSheet(from: .bottom)
present(toVc, animated: true)
将以下行添加到您的 Podfile
pod 'VDAnimation'
然后先从 podfile 目录运行 pod update 。
创建一个 Package.swift 文件。
// swift-tools-version:5.0
import PackageDescription
let package = Package(
name: "SomeProject",
dependencies: [
.package(url: "https://github.com/dankinsoid/VDAnimation.git", from: "1.51.0")
],
targets: [
.target(name: "SomeProject", dependencies: ["VDAnimation"])
]
)
$ swift build
dankinsoid, voidilov@gmail.com
VDAnimation 在 MIT 许可证下可用。 有关更多信息,请参见 LICENSE 文件。