Forked from CodingMeSwiftly/UIView+AnimationContext.swift
Created
August 10, 2019 13:36
-
-
Save steipete/d54c383c8337c336f6bd7d78993a153f 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