ActivityIndicatorAnimationTriforce.swift 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //
  2. // ActivityIndicatorAnimationTriforce.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 ActivityIndicatorAnimationTriforce: ActivityIndicatorAnimating {
  10. // MARK: Properties
  11. fileprivate let duration: CFTimeInterval = 3 / 2
  12. #if TRIFORCE
  13. public var trueColor = true
  14. #else
  15. public var trueColor = false
  16. #endif
  17. // MARK: ActivityIndicatorAnimating
  18. public func configureAnimation(in layer: CALayer, size: CGSize, color: UIColor) {
  19. let x = (layer.bounds.size.width - size.width) / 2
  20. let y = (layer.bounds.size.height - size.height) / 2
  21. let triforceSize = CGSize(width: size.width / 2, height: size.height)
  22. let power = ActivityIndicatorShape.triangle.makeLayer(size: triforceSize, color: trueColor ? .red: color)
  23. power.frame = CGRect(x: layer.bounds.size.width / 4, y: -layer.bounds.size.height / 2, width: triforceSize.width, height: triforceSize.height)
  24. power.add(defaultAnimation, forKey: "animation")
  25. layer.addSublayer(power)
  26. let wisdom = ActivityIndicatorShape.triangle.makeLayer(size: triforceSize, color: trueColor ? .blue: color)
  27. wisdom.frame = CGRect(x: x, y: y, width: triforceSize.width, height: triforceSize.height)
  28. wisdom.add(defaultAnimation, forKey: "animation")
  29. layer.addSublayer(wisdom)
  30. let courage = ActivityIndicatorShape.triangle.makeLayer(size: triforceSize, color: trueColor ? .green: color)
  31. courage.frame = CGRect(x: layer.bounds.size.width / 2, y: y, width: triforceSize.width, height: triforceSize.height)
  32. courage.add(defaultAnimation, forKey: "animation")
  33. layer.addSublayer(courage)
  34. }
  35. }
  36. // MARK: - Setup
  37. private extension ActivityIndicatorAnimationTriforce {
  38. var defaultAnimation: CAKeyframeAnimation {
  39. let timingFunction: TimingFunctionType = .linear
  40. let animation = CAKeyframeAnimation(keyPath: .transform)
  41. animation.keyTimes = [0, 1]
  42. animation.timingFunctionsType = [timingFunction, timingFunction]
  43. animation.values = [
  44. NSValue(caTransform3D: CATransform3DMakeRotation(0, 0, 1, 0)),
  45. NSValue(caTransform3D: CATransform3DMakeRotation(CGFloat.pi, 0, 1, 0))
  46. ]
  47. animation.duration = duration
  48. animation.repeatCount = .infinity
  49. animation.isRemovedOnCompletion = false
  50. return animation
  51. }
  52. }