This repository provides a new declarative way to describe animations
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()
simple UIKit animation, it's initialized by closure
UIViewAnimate {
...
}
.duration(0.3)
.start()
simple SwiftUI animation, it's initialized by closure
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 animations running one after another
Sequential {
Animate { ... }.duration(relative: 0.5)
Interval(0.1)
Parallel { ... }
}
.duration(1)
.start()
parallel animations running simultaneously
Parallel {
Animate { ... }.duration(relative: 0.5)
Sequential { ... }
}
.duration(1)
.start()
time interval
Interval(1)
any block of code, always zero duration
Instant {
...
}
CADisplayLink
wrapper
TimerAnimation { progress in
...
}
method .start()
or .delegate()
returns AnimationDelegateProtocol
object
.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)
- sets the animation duration in seconds.duration(relative: Double)
- sets the animation duration relative to the parent animation in 0...1.curve(BezierCurve)
- sets the animation curve.spring(dampingRatio: CGFloat = 0.3)
- sets spring animation curve (only forUIViewAnimate
).repeat()
,.repeat(Int)
- repeat animation.autoreverse()
,.autoreverse(repeat: Int)
- autoreverse animation.reversed()
- reversed animation.ca
-UIView
,CALayer
andView
,Binding
extension to describe an animation of properties
someView.ca.backgroundColor(.white).layer.cornerRadius(8).tintColor(.red).duration(0.3).start()
VDAnimation provides easy way to describe UIViewController
transitions.
VDAnimation also supports transitions like Keynote's Magic Move
or Hero
. It checks the .transition.id
property on all source and destination views. Every matched view pair is then automatically transitioned from its old state to its new state.
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)
Add the following line to your Podfile:
pod 'VDAnimation'
and run pod update
from the podfile directory first.
Create a Package.swift
file.
// 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 is available under the MIT license. See the LICENSE file for more info.