Forked from CodingMeSwiftly/UIView+AnimationContext.swift
Created
September 25, 2019 12:50
-
-
Save rxtr007/cfbb892357e485ce1252a48c0691c164 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