Forked from CodingMeSwiftly/UIView+AnimationContext.swift
Created
August 10, 2019 13:36
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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