Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save steipete/83898f3870dbedc417f3a8f8a592c019 to your computer and use it in GitHub Desktop.
Save steipete/83898f3870dbedc417f3a8f8a592c019 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