Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions FluidInterfaces/FluidInterfaces/Spring.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class SpringInterfaceViewController: InterfaceViewController {
sliderView.sliderFinishedMovingAction = { self.resetAnimation() }
return sliderView
}()

private var dampingRatio: CGFloat = 0.5
private var frequencyResponse: CGFloat = 1

Expand All @@ -67,22 +67,30 @@ class SpringInterfaceViewController: InterfaceViewController {
springView.bottomAnchor.constraint(equalTo: dampingSliderView.topAnchor, constant: -80).isActive = true

animateView()

}

private var animator = UIViewPropertyAnimator()

private var dwi: DispatchWorkItem?

/// Repeatedly animates the view using the current `dampingRatio` and `frequencyResponse`.
private func animateView() {
dwi?.cancel()

let timingParameters = UISpringTimingParameters(damping: dampingRatio, response: frequencyResponse)
animator = UIViewPropertyAnimator(duration: 0, timingParameters: timingParameters)
animator.addAnimations {
let translation = self.view.bounds.width - 2 * self.margin - 80
self.springView.transform = CGAffineTransform(translationX: translation, y: 0)
}
animator.addCompletion { _ in
self.springView.transform = .identity
self.animateView()
let dwi = DispatchWorkItem {
[weak self] in
self?.springView.transform = .identity
self?.animateView()
}
self.dwi = dwi
DispatchQueue.main.asyncAfter(deadline: .now() + 3, execute: dwi)
}
animator.startAnimation()
}
Expand Down