Skip to content

Instantly share code, notes, and snippets.

@chriseidhof
Created June 6, 2023 09:16
Show Gist options
  • Save chriseidhof/42c6f17f6fef1e7d804b79fd8972077c to your computer and use it in GitHub Desktop.
Save chriseidhof/42c6f17f6fef1e7d804b79fd8972077c to your computer and use it in GitHub Desktop.
//
import SwiftUI
import Observation
// easeInBounce
//
// https://easings.net
// https://gist.github.com/girish3/11167208
struct MyLinearAnimation: CustomAnimation {
var duration: TimeInterval
func animate<V>(value: V, time: TimeInterval, context: inout AnimationContext<V>) -> V? where V : VectorArithmetic {
if time > duration { return nil }
let t = time/duration
var progress: Double
if t < (1/2.75) {
progress = 7.5625 * t * t
} else if t < (2/2.75) {
let t2 = t-(1.5/2.75)
progress = 7.5625 * t2 * t2 + 0.75
} else if t < (2.5/2.75) {
let t2 = t - (2.25/2.75)
progress = (7.5625 * t2 * t2 + 0.9375)
} else {
let t2 = t - (2.625/2.75)
progress = 1 * (7.5625 * t2 * t2 + 0.984375)
}
print(progress)
let result = value.scaled(by: progress)
return result
}
}
struct ContentView: View {
@State var active = false
var body: some View {
Color.red
.frame(width: 50, height: active ? 100 : 50)
.frame(maxHeight: .infinity, alignment: active ? .top : .bottom)
.contentShape(.rect)
.onTapGesture {
withAnimation(.init(MyLinearAnimation(duration: 2))) {
active.toggle()
} completion: {
print("Completed")
}
}
}
}
#Preview {
ContentView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment