Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
gaetanzanella committed Dec 21, 2018
2 parents 29c86fb + b4582bd commit 5a5de0d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.0.1 (21 December 2018)

- Remove initial velocity consideration in `SpringOverlayTranslationAnimationController` (Issue #3)

## 1.0.0 (5 December 2018)

First release
2 changes: 1 addition & 1 deletion OverlayContainer.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'OverlayContainer'
s.version = '1.0.0'
s.version = '1.0.1'
s.summary = 'OverlayContainer is a UI library which makes it easier to develop overlay based interfaces.'
s.swift_version = "4.2"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@
import Foundation

private struct Constant {
static let defaultDamping: CGFloat = 0.7
static let defaultMass: CGFloat = 1
static let defaultDamping: CGFloat = 0.6
static let defaultResponse: CGFloat = 0.3
static let minimumVelocityConsideration: CGFloat = 500
static let minimumTranslationDuration: TimeInterval = 0.1
static let maximumTranslationDuration: TimeInterval = 0.5
static let minimumDamping: CGFloat = 1
static let minimumVelocityConsideration: CGFloat = 150
static let maximumVelocityConsideration: CGFloat = 3000
}

/// An `OverlayAnimatedTransitioning` implementation based on `UISpringTimingParameters`.
public class SpringOverlayTranslationAnimationController: OverlayAnimatedTransitioning {

public var mass: CGFloat = Constant.defaultMass
public var damping: CGFloat = Constant.defaultDamping
public var response: CGFloat = Constant.defaultResponse

Expand All @@ -28,29 +30,29 @@ public class SpringOverlayTranslationAnimationController: OverlayAnimatedTransit
// MARK: - OverlayAnimatedTransitioning

public func interruptibleAnimator(using context: OverlayContainerContextTransitioning) -> UIViewImplicitlyAnimating {
let targetHeight = context.targetNotchHeight
let distance = abs(targetHeight - context.overlayTranslationHeight)
let timing = UISpringTimingParameters(
damping: damping,
response: response,
initialVelocity: context.velocity
let velocity = min(
Constant.maximumVelocityConsideration,
max(abs(context.velocity.y), Constant.minimumVelocityConsideration)
)
let duration = max(
min(TimeInterval(distance / abs(context.velocity.y)), Constant.maximumTranslationDuration),
Constant.minimumTranslationDuration
let velocityRange = Constant.maximumVelocityConsideration - Constant.minimumVelocityConsideration
let normalizedVelocity = (velocity - Constant.minimumVelocityConsideration) / velocityRange
let normalizedDamping = normalizedVelocity * (damping - Constant.minimumDamping) + Constant.minimumDamping
let timing = UISpringTimingParameters(
damping: normalizedDamping,
response: Constant.defaultResponse,
mass: mass
)
return UIViewPropertyAnimator(
duration: duration,
duration: 0, // duration is ignored when using `UISpringTimingParameters.init(mass:stiffness:damping:initialVelocity)`
timingParameters: timing
)
}
}

extension UISpringTimingParameters {
convenience init(damping: CGFloat, response: CGFloat, initialVelocity: CGPoint = .zero) {
convenience init(damping: CGFloat, response: CGFloat, mass: CGFloat) {
let stiffness = pow(2 * .pi / response, 2)
let damp = 4 * .pi * damping / response
let vector = CGVector(dx: abs(initialVelocity.x / 1000), dy: abs(initialVelocity.y) / 1000)
self.init(mass: 1, stiffness: stiffness, damping: damp, initialVelocity: vector)
self.init(mass: mass, stiffness: stiffness, damping: damp, initialVelocity: .zero)
}
}

0 comments on commit 5a5de0d

Please sign in to comment.