ActivityIndicatorAnimationBallClipRotate.swift 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 ActivityIndicatorAnimationBallClipRotate: ActivityIndicatorAnimating {
  7. // MARK: Properties
  8. fileprivate let duration: CFTimeInterval = 0.75
  9. // MARK: ActivityIndicatorAnimating
  10. public func configureAnimation(in layer: CALayer, size: CGSize, color: UIColor) {
  11. // Draw circle
  12. let circle = ActivityIndicatorShape.ringThirdFour.makeLayer(size: CGSize(width: size.width, height: size.height), color: color)
  13. let frame = CGRect(x: (layer.bounds.size.width - size.width) / 2,
  14. y: (layer.bounds.size.height - size.height) / 2,
  15. width: size.width,
  16. height: size.height)
  17. circle.frame = frame
  18. circle.add(animation, forKey: "animation")
  19. layer.addSublayer(circle)
  20. }
  21. }
  22. // MARK: - Setup
  23. private extension ActivityIndicatorAnimationBallClipRotate {
  24. var scaleAnimation: CAKeyframeAnimation {
  25. let scaleAnimation = CAKeyframeAnimation(keyPath: .scale)
  26. scaleAnimation.keyTimes = [0, 0.5, 1]
  27. scaleAnimation.values = [1, 0.6, 1]
  28. return scaleAnimation
  29. }
  30. var rotateAnimation: CAKeyframeAnimation {
  31. let rotateAnimation = CAKeyframeAnimation(keyPath: .rotationZ)
  32. rotateAnimation.keyTimes = scaleAnimation.keyTimes
  33. rotateAnimation.values = [0, CGFloat.pi, 2 * CGFloat.pi]
  34. return rotateAnimation
  35. }
  36. var animation: CAAnimationGroup {
  37. let animation = CAAnimationGroup()
  38. animation.animations = [scaleAnimation, rotateAnimation]
  39. animation.timingFunctionType = .linear
  40. animation.duration = duration
  41. animation.repeatCount = .infinity
  42. animation.isRemovedOnCompletion = false
  43. return animation
  44. }
  45. }