ActivityIndicatorAnimationTripleGear.swift 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // ActivityIndicatorAnimationTripleGear.swift
  3. // IBAnimatable
  4. //
  5. // Created by phimage on 04/05/2018.
  6. // Copyright © 2018 IBAnimatable. All rights reserved.
  7. //
  8. import UIKit
  9. public class ActivityIndicatorAnimationTripleGear: ActivityIndicatorAnimating {
  10. // MARK: Properties
  11. fileprivate let duration: CFTimeInterval = 3
  12. // MARK: ActivityIndicatorAnimating
  13. public func configureAnimation(in layer: CALayer, size: CGSize, color: UIColor) {
  14. let x = (layer.bounds.size.width - size.width) / 2 + size.width / 8
  15. let y = (layer.bounds.size.height - size.height) / 2 + size.height / 8
  16. let mask: MaskType = .gear(radius: Double(layer.bounds.size.width / 20), cogs: 8)
  17. let gear = ActivityIndicatorShape.mask(type: mask).makeLayer(size: size / 2, color: color)
  18. gear.frame = CGRect(x: x, y: y, width: size.width / 2, height: size.height / 2)
  19. let animation = defaultAnimation
  20. animation.byValue = -Float.pi * 2
  21. gear.add(animation, forKey: "animation")
  22. layer.addSublayer(gear)
  23. let mask2: MaskType = .gear(radius: Double(layer.bounds.size.width / 30), cogs: 8)
  24. let gear2 = ActivityIndicatorShape.mask(type: mask2).makeLayer(size: size / 3, color: color)
  25. gear2.frame = CGRect(x: x + size.width / 4, y: y + size.height / 2, width: size.width / 3, height: size.height / 3)
  26. gear2.add(defaultAnimation, forKey: "animation")
  27. layer.addSublayer(gear2)
  28. let mask3: MaskType = .gear(radius: Double(layer.bounds.size.width / 40), cogs: 8)
  29. let gear3 = ActivityIndicatorShape.mask(type: mask3).makeLayer(size: size / 4, color: color)
  30. gear3.frame = CGRect(x: x + size.width / 2, y: y + size.height / 4, width: size.width / 4, height: size.height / 4)
  31. gear3.add(defaultAnimation, forKey: "animation")
  32. layer.addSublayer(gear3)
  33. }
  34. }
  35. // MARK: - Setup
  36. private extension ActivityIndicatorAnimationTripleGear {
  37. var defaultAnimation: CABasicAnimation {
  38. let rotationAnimation = CABasicAnimation(keyPath: .rotation)
  39. rotationAnimation.byValue = Float.pi * 2
  40. rotationAnimation.timingFunctionType = .linear
  41. rotationAnimation.duration = duration
  42. rotationAnimation.repeatCount = .infinity
  43. rotationAnimation.isRemovedOnCompletion = false
  44. return rotationAnimation
  45. }
  46. }
  47. private extension CGSize {
  48. static func / (size: CGSize, by: CGFloat) -> CGSize {
  49. return CGSize(width: size.width / by, height: size.height / by)
  50. }
  51. }