Created
January 29, 2020 09:33
-
-
Save devxoul/76a9f79028502e3d85d2e64d28de43ca to your computer and use it in GitHub Desktop.
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
import UIKit | |
import PlaygroundSupport | |
let width: CGFloat = 200 | |
let height: CGFloat = 100 | |
let spacing: CGFloat = 10 | |
let numberOfViews: Int = 5 | |
let canvas = UIView(frame: CGRect( | |
x: 0, | |
y: 0, | |
width: width, | |
height: height * CGFloat(numberOfViews) + spacing * CGFloat(numberOfViews - 1) | |
)) | |
canvas.backgroundColor = .white | |
PlaygroundPage.current.liveView = canvas | |
final class SkeletonView: UIView { | |
func startAnimation(timeOffset: CFTimeInterval = 0) { | |
let animation = CABasicAnimation(keyPath: #keyPath(CALayer.backgroundColor)) | |
animation.fromValue = UIColor.lightGray.cgColor | |
animation.toValue = UIColor.gray.cgColor | |
animation.duration = 1 | |
animation.autoreverses = true | |
animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut) | |
animation.repeatCount = .infinity | |
animation.isRemovedOnCompletion = false | |
animation.timeOffset = CFAbsoluteTimeGetCurrent() | |
self.layer.add(animation, forKey: nil) | |
} | |
} | |
for i in 0..<numberOfViews { | |
let view = SkeletonView(frame: CGRect( | |
x: 0, | |
y: CGFloat(i) * (height + spacing), | |
width: width, | |
height: height | |
)) | |
canvas.addSubview(view) | |
DispatchQueue.main.asyncAfter(deadline: .now() + TimeInterval(i) / 3) { | |
view.startAnimation() | |
} | |
} |
Author
devxoul
commented
Jan 29, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment