ActivityIndicatorAnimationRupee.swift 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // ActivityIndicatorAnimationRupee.swift
  3. // IBAnimatable
  4. //
  5. // Created by phimage on 07/05/2018.
  6. // Copyright © 2018 IBAnimatable. All rights reserved.
  7. //
  8. import UIKit
  9. public class ActivityIndicatorAnimationRupee: ActivityIndicatorAnimating {
  10. // MARK: Properties
  11. fileprivate let duration: CFTimeInterval = 1
  12. fileprivate let maskType: MaskType = .polygon(sides: 6)
  13. // MARK: ActivityIndicatorAnimating
  14. public func configureAnimation(in layer: CALayer, size: CGSize, color: UIColor) {
  15. let maskSpacing: CGFloat = -2
  16. let maskSize = (size.width - 4 * maskSpacing) / 5
  17. let x = (layer.bounds.size.width - size.width) / 2
  18. let y = (layer.bounds.size.height - size.height) / 2
  19. let beginTime = layer.currentMediaTime
  20. let beginTimes: [CFTimeInterval] = [0.0, 0.5, 0.0, 0.5, 0.0, 0.0, 0.5, 0.0]
  21. let animation = defaultAnimation
  22. // Draw shapes
  23. for i in 0 ..< 8 {
  24. if i % 4 != 0 { // remove left and right
  25. let shapeLayer = makeLayer(angle: .pi / 4 * CGFloat(i),
  26. size: maskSize,
  27. origin: CGPoint(x: x, y: y),
  28. containerSize: size,
  29. color: color)
  30. animation.beginTime = beginTime + beginTimes[i]
  31. shapeLayer.add(animation, forKey: "animation")
  32. layer.addSublayer(shapeLayer)
  33. }
  34. }
  35. }
  36. func makeLayer(angle: CGFloat, size: CGFloat, origin: CGPoint, containerSize: CGSize, color: UIColor) -> CALayer {
  37. let radius = containerSize.width / 2 - size / 2
  38. let layer = ActivityIndicatorShape.mask(type: maskType).makeLayer(size: CGSize(width: size, height: size), color: color)
  39. let frame = CGRect(
  40. x: origin.x + radius * (cos(angle) + 1),
  41. y: origin.y + radius * (sin(angle) + 1),
  42. width: size,
  43. height: size)
  44. layer.frame = frame
  45. return layer
  46. }
  47. }
  48. // MARK: - Setup
  49. private extension ActivityIndicatorAnimationRupee {
  50. var defaultAnimation: CAAnimationGroup {
  51. let animation = CAAnimationGroup()
  52. animation.animations = [scaleAnimation, opacityAnimation]
  53. animation.timingFunctionType = .linear
  54. animation.duration = duration
  55. animation.repeatCount = .infinity
  56. animation.isRemovedOnCompletion = false
  57. return animation
  58. }
  59. var scaleAnimation: CAKeyframeAnimation {
  60. let scaleAnimation = CAKeyframeAnimation(keyPath: .scale)
  61. scaleAnimation.keyTimes = [0, 0.5, 1]
  62. scaleAnimation.values = [1, 0.4, 1]
  63. scaleAnimation.duration = duration
  64. return scaleAnimation
  65. }
  66. var opacityAnimation: CAKeyframeAnimation {
  67. let opacityAnimation = CAKeyframeAnimation(keyPath: .opacity)
  68. opacityAnimation.keyTimes = [0, 0.5, 1]
  69. opacityAnimation.values = [1, 0.3, 1]
  70. opacityAnimation.duration = duration
  71. return opacityAnimation
  72. }
  73. }