Skip to content

Added 3s delay before reseting and repeating Spring animation. #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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