VDAnimation
描述
这个仓库提供了一种描述动画的新声明式方法
示例
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()
使用方法
UIViewAnimate
简单的UIKit动画,通过闭包初始化
UIViewAnimate {
...
}
.duration(0.3)
.start()
Animate
简单的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
逐个运行的连续动画
Sequential {
Animate { ... }.duration(relative: 0.5)
Interval(0.1)
Parallel { ... }
}
.duration(1)
.start()
Parallel
同时运行的并行动画
Parallel {
Animate { ... }.duration(relative: 0.5)
Sequential { ... }
}
.duration(1)
.start()
Interval
时间间隔
Interval(1)
Instant
任何代码块,总是零时长
Instant {
...
}
TimerAnimation
CADisplayLink
包装器
TimerAnimation { progress in
...
}
交互式
方法 .start()
或 .delegate()
返回 AnimationDelegateProtocol
对象
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, [email protected]
许可证
VDAnimation 在 MIT 许可证下提供。更多信息请参阅 LICENSE 文件。