Skip to content

Instantly share code, notes, and snippets.

@devxoul
Created January 29, 2020 09:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devxoul/76a9f79028502e3d85d2e64d28de43ca to your computer and use it in GitHub Desktop.
Save devxoul/76a9f79028502e3d85d2e64d28de43ca to your computer and use it in GitHub Desktop.
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()
}
}
@devxoul
Copy link
Author

devxoul commented Jan 29, 2020

skeleton mov

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment