Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save avdwerff/7d607c1d568d4faf38853b33499de033 to your computer and use it in GitHub Desktop.
Save avdwerff/7d607c1d568d4faf38853b33499de033 to your computer and use it in GitHub Desktop.
import UIKit
class ActivityIndicatorView: UIView {
private lazy var gradientLayer: CAGradientLayer = {
let gradient = CAGradientLayer()
gradient.locations = [0.5, 1]
gradient.startPoint = CGPointMake(0.0, 0.0)
gradient.endPoint = CGPointMake(1.0, 0.8)
gradient.anchorPoint = CGPointMake(0, 0)
gradient.contentsScale = UIScreen.mainScreen().scale
return gradient
}()
private lazy var circlePathLayer: CAShapeLayer = {
let layer = CAShapeLayer()
layer.fillColor = UIColor.clearColor().CGColor
layer.strokeColor = UIColor.blackColor().CGColor
layer.lineWidth = 3
layer.anchorPoint = CGPointMake(0, 0)
return layer
}()
var colors: [UIColor] = [UIColor.whiteColor(), UIColor.clearColor()]
override init(frame: CGRect) {
super.init(frame: frame)
}
convenience init(color: UIColor = UIColor.whiteColor()) {
self.init(frame: CGRectZero)
self.colors = [color, UIColor(hex: 0xffffff, alpha: 0)]
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
private func setup() {
gradientLayer.colors = colors
backgroundColor = UIColor.clearColor()
gradientLayer.position = CGPoint(x: CGRectGetMidX(bounds), y: CGRectGetMidY(bounds))
circlePathLayer.frame = gradientLayer.bounds
gradientLayer.mask = circlePathLayer
layer.addSublayer(gradientLayer)
}
override func layoutSubviews() {
super.layoutSubviews()
gradientLayer.colors = colors.map { $0.CGColor }
circlePathLayer.path = UIBezierPath(roundedRect: CGRectInset(gradientLayer.bounds, 3, 3), cornerRadius: gradientLayer.bounds.size.width/2).CGPath
gradientLayer.bounds = bounds
rotate()
}
func rotate() {
let rotation: CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
rotation.toValue = NSNumber(double: M_PI * 2)
rotation.duration = 1
rotation.cumulative = true
rotation.delegate = self
rotation.repeatCount = FLT_MAX
self.layer.addAnimation(rotation, forKey: "rotationAnimation")
}
override func animationDidStop(anim: CAAnimation, finished flag: Bool) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment