Forked from arturchabera/skeletonViewProtocol.md
Last active
February 1, 2023 16:12
-
-
Save temptempest/c003841b77664523908ecfcd84ad002d to your computer and use it in GitHub Desktop.
Adding Skeleton Animation to a UIView
This file contains hidden or 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
| /// Adding Skeleton Animation to a UIView | |
| /// Usage: view.showSkeleton(backgroundColor: UIColor.systemGray6, highlightColor: UIColor.systemGray3) | |
| /// view.hideSkeleton() | |
| extension UIView { | |
| public enum SkeletonPosition { | |
| case horizontal | |
| case vertical | |
| case horizontalWithAngle | |
| } | |
| private var skeletonLayerName: String { | |
| return "skeletonLayerName" | |
| } | |
| private var skeletonGradientName: String { | |
| return "skeletonGradientName" | |
| } | |
| func showSkeleton(duration: Double = 2, | |
| backgroundColor: UIColor, | |
| highlightColor: UIColor, | |
| position: SkeletonPosition = .horizontal) { | |
| let screenBounds = UIScreen.main.bounds | |
| var angle: CGFloat = 0 | |
| var animationKey = "transform.translation.x" | |
| var startPoint = CGPoint(x: 0.0, y: 0.5) | |
| var endPoint = CGPoint(x: 1.0, y: 0.5) | |
| var fromValue = -screenBounds.width | |
| var toValue = screenBounds.width | |
| switch position { | |
| case .horizontal: break | |
| case .vertical: | |
| animationKey = "transform.translation.y" | |
| startPoint = CGPoint(x: 0.5, y: 0) | |
| endPoint = CGPoint(x: 0.5, y: 1) | |
| fromValue = -screenBounds.height | |
| toValue = screenBounds.height | |
| case .horizontalWithAngle: | |
| angle = 45 * CGFloat.pi / 180 | |
| } | |
| let skeletonLayer = CALayer() | |
| skeletonLayer.backgroundColor = backgroundColor.cgColor | |
| skeletonLayer.name = skeletonLayerName | |
| skeletonLayer.anchorPoint = .zero | |
| skeletonLayer.frame.size = screenBounds.size | |
| let gradientLayer = CAGradientLayer() | |
| gradientLayer.colors = [ | |
| backgroundColor.cgColor, | |
| highlightColor.cgColor, | |
| backgroundColor.cgColor | |
| ] | |
| gradientLayer.startPoint = startPoint | |
| gradientLayer.endPoint = endPoint | |
| gradientLayer.frame = screenBounds | |
| gradientLayer.transform = CATransform3DMakeRotation(angle, 0, 0, 1) | |
| gradientLayer.name = skeletonGradientName | |
| layer.mask = skeletonLayer | |
| layer.addSublayer(skeletonLayer) | |
| layer.addSublayer(gradientLayer) | |
| clipsToBounds = true | |
| let animation = CABasicAnimation(keyPath: animationKey) | |
| animation.duration = CFTimeInterval(duration) | |
| animation.fromValue = fromValue | |
| animation.toValue = toValue | |
| animation.repeatCount = .infinity | |
| animation.autoreverses = false | |
| animation.fillMode = .forwards | |
| gradientLayer.add(animation, forKey: "AwesomeAnimation") | |
| } | |
| func hideSkeleton() { | |
| layer.sublayers?.removeAll { | |
| $0.name == skeletonLayerName || $0.name == skeletonGradientName | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment