Skip to content

Instantly share code, notes, and snippets.

@CodingMeSwiftly
Created August 10, 2019 10:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save CodingMeSwiftly/0da292b6e52e0bce10099868c1a0f617 to your computer and use it in GitHub Desktop.
Save CodingMeSwiftly/0da292b6e52e0bce10099868c1a0f617 to your computer and use it in GitHub Desktop.
An extension on UIView to retrieve information about the animation context the view is currently in.
import UIKit
extension UIView {
struct AnimationContext {
public let duration: TimeInterval
public let timingParameters: UITimingCurveProvider?
}
}
extension UIView {
var animationContext: UIView.AnimationContext? {
guard let action = action(for: layer, forKey: "backgroundColor") as? NSObject, !(action is NSNull) else { return nil }
let duration: TimeInterval
let timingParameters: UITimingCurveProvider?
if let animation = action as? CAAnimation {
duration = animation.duration
timingParameters = animation.timingFunction
} else if let animator = action.value(forKeyPath: "animationObject") as? UIViewPropertyAnimator {
duration = animator.duration
timingParameters = animator.timingParameters
} else {
return nil
}
return UIView.AnimationContext(duration: duration, timingParameters: timingParameters)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment