ActivityIndicatorAnimationBallClipRotatePulse.swift 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //
  2. // Created by Tom Baranes on 23/08/16.
  3. // Copyright (c) 2016 IBAnimatable. All rights reserved.
  4. //
  5. import UIKit
  6. public class ActivityIndicatorAnimationBallClipRotatePulse: ActivityIndicatorAnimating {
  7. // MARK: Properties
  8. fileprivate let duration: CFTimeInterval = 1
  9. fileprivate let timingFunction = CAMediaTimingFunction(controlPoints: 0.09, 0.57, 0.49, 0.9)
  10. // MARK: ActivityIndicatorAnimating
  11. public func configureAnimation(in layer: CALayer, size: CGSize, color: UIColor) {
  12. animateSmallCircle(duration: duration, timingFunction: timingFunction, layer: layer, size: size, color: color)
  13. animateBigCircle(duration: duration, timingFunction: timingFunction, layer: layer, size: size, color: color)
  14. }
  15. }
  16. // MARK: Small circle
  17. private extension ActivityIndicatorAnimationBallClipRotatePulse {
  18. func animateSmallCircle(duration: CFTimeInterval, timingFunction: CAMediaTimingFunction, layer: CALayer, size: CGSize, color: UIColor) {
  19. let animation = makeSmallCircleAnimation()
  20. let circleSize = size.width / 2
  21. let circle = ActivityIndicatorShape.circle.makeLayer(size: CGSize(width: circleSize, height: circleSize), color: color)
  22. let frame = CGRect(x: (layer.bounds.size.width - circleSize) / 2,
  23. y: (layer.bounds.size.height - circleSize) / 2,
  24. width: circleSize,
  25. height: circleSize)
  26. circle.frame = frame
  27. circle.add(animation, forKey: "animation")
  28. layer.addSublayer(circle)
  29. }
  30. func makeSmallCircleAnimation() -> CAKeyframeAnimation {
  31. let animation = CAKeyframeAnimation(keyPath: .scale)
  32. animation.keyTimes = [0, 0.3, 1]
  33. animation.timingFunctions = [timingFunction, timingFunction]
  34. animation.values = [1, 0.3, 1]
  35. animation.duration = duration
  36. animation.repeatCount = .infinity
  37. animation.isRemovedOnCompletion = false
  38. return animation
  39. }
  40. }
  41. // MARK: Big circle
  42. private extension ActivityIndicatorAnimationBallClipRotatePulse {
  43. func animateBigCircle(duration: CFTimeInterval, timingFunction: CAMediaTimingFunction, layer: CALayer, size: CGSize, color: UIColor) {
  44. let animation = makeBigCircleAnimation()
  45. let circle = ActivityIndicatorShape.ringTwoHalfVertical.makeLayer(size: size, color: color)
  46. let frame = CGRect(x: (layer.bounds.size.width - size.width) / 2,
  47. y: (layer.bounds.size.height - size.height) / 2,
  48. width: size.width,
  49. height: size.height)
  50. circle.frame = frame
  51. circle.add(animation, forKey: "animation")
  52. layer.addSublayer(circle)
  53. }
  54. func makeBigCircleAnimation() -> CAAnimationGroup {
  55. let animation = CAAnimationGroup()
  56. animation.animations = [scaleAnimation, rotateAnimation]
  57. animation.duration = duration
  58. animation.repeatCount = .infinity
  59. animation.isRemovedOnCompletion = false
  60. return animation
  61. }
  62. var scaleAnimation: CAKeyframeAnimation {
  63. let scaleAnimation = CAKeyframeAnimation(keyPath: .scale)
  64. scaleAnimation.keyTimes = [0, 0.5, 1]
  65. scaleAnimation.timingFunctions = [timingFunction, timingFunction]
  66. scaleAnimation.values = [1, 0.6, 1]
  67. scaleAnimation.duration = duration
  68. return scaleAnimation
  69. }
  70. var rotateAnimation: CAKeyframeAnimation {
  71. let rotateAnimation = CAKeyframeAnimation(keyPath: .rotationZ)
  72. rotateAnimation.keyTimes = scaleAnimation.keyTimes
  73. rotateAnimation.timingFunctions = [timingFunction, timingFunction]
  74. rotateAnimation.values = [0, CGFloat.pi, 2 * CGFloat.pi]
  75. rotateAnimation.duration = duration
  76. return rotateAnimation
  77. }
  78. }