ActivityIndicatorAnimationCircleStrokeSpin.swift 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //
  2. // ActivityIndicatorAnimationCircleStrokeSpin.swift
  3. // IBAnimatable
  4. //
  5. // Created by phimage on 02/05/2018.
  6. // Copyright © 2018 IBAnimatable. All rights reserved.
  7. //
  8. import UIKit
  9. public class ActivityIndicatorAnimationCircleStrokeSpin: ActivityIndicatorAnimating {
  10. // MARK: Properties
  11. let beginTime: Double = 0.5
  12. let strokeStartDuration: Double = 1.2
  13. let strokeEndDuration: Double = 0.7
  14. let timingFunction: CAMediaTimingFunction = CAMediaTimingFunction(controlPoints: 0.4, 0.0, 0.2, 1.0)
  15. // MARK: ActivityIndicatorAnimating
  16. public func configureAnimation(in layer: CALayer, size: CGSize, color: UIColor) {
  17. let x = (layer.bounds.size.width - size.width) / 2
  18. let y = (layer.bounds.size.height - size.height) / 2
  19. let circle = ActivityIndicatorShape.stroke.makeLayer(size: size, color: color)
  20. circle.frame = CGRect(x: x, y: y, width: size.width, height: size.height)
  21. circle.add(defaultAnimation, forKey: "animation")
  22. layer.addSublayer(circle)
  23. }
  24. }
  25. // MARK: - Setup
  26. private extension ActivityIndicatorAnimationCircleStrokeSpin {
  27. var defaultAnimation: CAAnimationGroup {
  28. let groupAnimation = CAAnimationGroup()
  29. groupAnimation.animations = [rotationAnimation, strokeEndAnimation, strokeStartAnimation]
  30. groupAnimation.duration = strokeStartDuration + beginTime
  31. groupAnimation.repeatCount = .infinity
  32. groupAnimation.isRemovedOnCompletion = false
  33. groupAnimation.fillMode = CAMediaTimingFillMode.forwards
  34. return groupAnimation
  35. }
  36. var rotationAnimation: CABasicAnimation {
  37. let rotationAnimation = CABasicAnimation(keyPath: .rotation)
  38. rotationAnimation.byValue = Float.pi * 2
  39. rotationAnimation.timingFunctionType = .linear
  40. return rotationAnimation
  41. }
  42. var strokeEndAnimation: CABasicAnimation {
  43. let strokeEndAnimation = CABasicAnimation(keyPath: .strokeEnd)
  44. strokeEndAnimation.duration = strokeEndDuration
  45. strokeEndAnimation.timingFunction = timingFunction
  46. strokeEndAnimation.fromValue = 0
  47. strokeEndAnimation.toValue = 1
  48. return strokeEndAnimation
  49. }
  50. var strokeStartAnimation: CABasicAnimation {
  51. let strokeStartAnimation = CABasicAnimation(keyPath: .strokeStart)
  52. strokeStartAnimation.duration = strokeStartDuration
  53. strokeStartAnimation.timingFunction = timingFunction
  54. strokeStartAnimation.fromValue = 0
  55. strokeStartAnimation.toValue = 1
  56. strokeStartAnimation.beginTime = beginTime
  57. return strokeStartAnimation
  58. }
  59. }