Skip to content

Instantly share code, notes, and snippets.

@Ibrahimhass
Last active November 23, 2018 21:46
Show Gist options
  • Save Ibrahimhass/705584269a24d4ea2983ef4e2f20579b to your computer and use it in GitHub Desktop.
Save Ibrahimhass/705584269a24d4ea2983ef4e2f20579b to your computer and use it in GitHub Desktop.
SVIndefiniteAnimatedView.m Converted Swift file
class IndefiniteAnimatedView : UIView {
private var activityIndicator : UIActivityIndicatorView?
private var strokeThickness : CGFloat?
private var strokeColor : UIColor?
private var indefinteAnimatedLayer : CAShapeLayer?
private var radius : CGFloat?
//MARK: - Setter Functions
@objc func setIndefinite(radius: CGFloat) {
if (self.radius != radius) {
self.radius = radius
self.getIndefinteAnimatedLayer().removeFromSuperlayer()
self.indefinteAnimatedLayer = nil
if superview != nil {
layoutAnimatedLayer()
}
}
}
//MARK: - Getter Functions
private func getIndefinteAnimatedLayer() -> CAShapeLayer {
if self.indefinteAnimatedLayer != nil {
return self.indefinteAnimatedLayer!
} else {
let localRingRadius : CGFloat = radius ?? 18
let localStrokeThickness : CGFloat = strokeThickness ?? 2
let localStrokeColor : UIColor = strokeColor ?? UIColor.black
let arcCenter = CGPoint(x: localRingRadius + localStrokeThickness / 2 + 5,
y: localRingRadius + localStrokeThickness / 2 + 5)
let smoothedPath = UIBezierPath(arcCenter: arcCenter,
radius: localRingRadius,
startAngle: -CGFloat.pi / 2,
endAngle: CGFloat.pi + CGFloat.pi / 2,
clockwise: true)
indefinteAnimatedLayer = CAShapeLayer()
indefinteAnimatedLayer?.contentsScale = UIScreen.main.scale
indefinteAnimatedLayer?.frame = CGRect.init(x: 0, y: 0, width: arcCenter.x * 2, height: arcCenter.y * 2)
indefinteAnimatedLayer?.fillColor = UIColor.clear.cgColor
indefinteAnimatedLayer?.strokeColor = localStrokeColor.cgColor
indefinteAnimatedLayer?.lineWidth = localStrokeThickness
indefinteAnimatedLayer?.lineCap = CAShapeLayerLineCap.round
indefinteAnimatedLayer?.lineJoin = CAShapeLayerLineJoin.bevel
indefinteAnimatedLayer?.path = smoothedPath.cgPath
let maskLayer = CALayer()
let image = #imageLiteral(resourceName: "angle-mask")
maskLayer.contents = image.cgImage
maskLayer.frame = indefinteAnimatedLayer!.bounds
indefinteAnimatedLayer?.mask = maskLayer
let animationDuration = TimeInterval.init(1)
let linearCurve = CAMediaTimingFunction.init(name: .linear)
let animation = CABasicAnimation.init(keyPath: "transform.rotation")
animation.fromValue = 0
animation.toValue = CGFloat.pi * 2
animation.duration = animationDuration
animation.timingFunction = linearCurve
animation.isRemovedOnCompletion = false
animation.repeatCount = .infinity
animation.fillMode = .forwards
animation.autoreverses = false
indefinteAnimatedLayer?.mask?.add(animation, forKey: "rotate")
let animationGroup = CAAnimationGroup.init()
animationGroup.duration = animationDuration
animationGroup.repeatCount = .infinity
animationGroup.isRemovedOnCompletion = false
animationGroup.timingFunction = linearCurve
let strokeStartAnimation = CABasicAnimation.init(keyPath: "strokeStart")
strokeStartAnimation.duration = animationDuration
strokeStartAnimation.fromValue = 0.015
strokeStartAnimation.toValue = 0.0001
animationGroup.animations = [strokeStartAnimation]
indefinteAnimatedLayer?.add(animationGroup, forKey: "progress")
}
return self.indefinteAnimatedLayer!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment