IndefiniteAnimatedView.swift 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // Converted to Swift 4 by Swiftify v4.2.29618 - https://objectivec2swift.com/
  3. //
  4. // IndefiniteAnimatedView.swift
  5. // SVProgressHUD, https://github.com/SVProgressHUD/SVProgressHUD
  6. //
  7. // Original Copyright (c) 2014-2018 Guillaume Campagna. All rights reserved.
  8. // Modified Copyright © 2018 Ibrahim Hassan. All rights reserved.
  9. //
  10. import UIKit
  11. class IndefiniteAnimatedView : UIView {
  12. private var activityIndicator : UIActivityIndicatorView?
  13. private var strokeThickness : CGFloat?
  14. private var strokeColor : UIColor?
  15. private var indefinteAnimatedLayer : CAShapeLayer?
  16. private var radius : CGFloat?
  17. override init(frame: CGRect) {
  18. super.init(frame: frame)
  19. if self.superview != nil {
  20. layoutAnimatedLayer()
  21. }
  22. }
  23. required init?(coder aDecoder: NSCoder) {
  24. fatalError("init(coder:) has not been implemented")
  25. }
  26. }
  27. //MARK: - Setter Functions
  28. extension IndefiniteAnimatedView {
  29. func setIndefinite(radius: CGFloat) {
  30. if (self.radius != radius) {
  31. self.radius = radius
  32. self.getIndefinteAnimatedLayer().removeFromSuperlayer()
  33. self.indefinteAnimatedLayer = nil
  34. if superview != nil {
  35. layoutAnimatedLayer()
  36. }
  37. }
  38. }
  39. func setIndefinite(strokeThickness : CGFloat) {
  40. self.strokeThickness = strokeThickness
  41. if let strkthickness = self.strokeThickness {
  42. getIndefinteAnimatedLayer().lineWidth = strkthickness
  43. }
  44. }
  45. func setIndefinite(strokeColor: UIColor) {
  46. self.strokeColor = strokeColor
  47. getIndefinteAnimatedLayer().strokeColor = strokeColor.cgColor
  48. }
  49. }
  50. //MARK: - Getter Functions
  51. extension IndefiniteAnimatedView {
  52. private func getIndefinteAnimatedLayer() -> CAShapeLayer {
  53. if self.indefinteAnimatedLayer != nil {
  54. return self.indefinteAnimatedLayer!
  55. } else {
  56. let localRingRadius : CGFloat = radius ?? 18
  57. let localStrokeThickness : CGFloat = strokeThickness ?? 2
  58. let localStrokeColor : UIColor = strokeColor ?? UIColor.black
  59. let arcCenter = CGPoint(x: localRingRadius + localStrokeThickness / 2 + 5, y: localRingRadius + localStrokeThickness / 2 + 5)
  60. let smoothedPath = UIBezierPath(arcCenter: arcCenter, radius: localRingRadius, startAngle: -CGFloat.pi / 2, endAngle: CGFloat.pi + CGFloat.pi / 2, clockwise: true)
  61. indefinteAnimatedLayer = CAShapeLayer()
  62. indefinteAnimatedLayer?.contentsScale = UIScreen.main.scale
  63. indefinteAnimatedLayer?.frame = CGRect.init(x: 0, y: 0, width: arcCenter.x * 2, height: arcCenter.y * 2)
  64. indefinteAnimatedLayer?.fillColor = UIColor.clear.cgColor
  65. indefinteAnimatedLayer?.strokeColor = localStrokeColor.cgColor
  66. indefinteAnimatedLayer?.lineWidth = localStrokeThickness
  67. indefinteAnimatedLayer?.lineCap = CAShapeLayerLineCap.round
  68. indefinteAnimatedLayer?.lineJoin = CAShapeLayerLineJoin.bevel
  69. indefinteAnimatedLayer?.path = smoothedPath.cgPath
  70. let maskLayer = CALayer()
  71. let image = loadImageBundle(named: "angle-mask")!
  72. maskLayer.contents = image.cgImage
  73. maskLayer.frame = indefinteAnimatedLayer!.bounds
  74. indefinteAnimatedLayer?.mask = maskLayer
  75. let animationDuration = TimeInterval.init(1)
  76. let linearCurve = CAMediaTimingFunction.init(name: .linear)
  77. let animation = CABasicAnimation.init(keyPath: "transform.rotation")
  78. animation.fromValue = 0
  79. animation.toValue = CGFloat.pi * 2
  80. animation.duration = animationDuration
  81. animation.timingFunction = linearCurve
  82. animation.isRemovedOnCompletion = false
  83. animation.repeatCount = .infinity
  84. animation.fillMode = .forwards
  85. animation.autoreverses = false
  86. indefinteAnimatedLayer?.mask?.add(animation, forKey: "rotate")
  87. let animationGroup = CAAnimationGroup.init()
  88. animationGroup.duration = animationDuration
  89. animationGroup.repeatCount = .infinity
  90. animationGroup.isRemovedOnCompletion = false
  91. animationGroup.timingFunction = linearCurve
  92. let strokeStartAnimation = CABasicAnimation.init(keyPath: "strokeStart")
  93. strokeStartAnimation.duration = animationDuration
  94. strokeStartAnimation.fromValue = 0.015
  95. strokeStartAnimation.toValue = 0.0001
  96. animationGroup.animations = [strokeStartAnimation]
  97. indefinteAnimatedLayer?.add(animationGroup, forKey: "progress")
  98. }
  99. return self.indefinteAnimatedLayer!
  100. }
  101. }
  102. //MARK: - ActivityIndicatorView Functions
  103. extension IndefiniteAnimatedView {
  104. func removeAnimationLayer() {
  105. for view in self.subviews {
  106. if let activityView = view as? UIActivityIndicatorView {
  107. activityView.removeFromSuperview()
  108. }
  109. }
  110. getIndefinteAnimatedLayer().removeFromSuperlayer()
  111. }
  112. func startAnimation() {
  113. if let activityIndicator = activityIndicator {
  114. self.addSubview(activityIndicator)
  115. activityIndicator.frame = CGRect.init(x: 8, y: 8, width: self.frame.size.width - 16, height: self.frame.size.height - 16)
  116. }
  117. }
  118. func stopActivityIndicator() {
  119. activityIndicator?.stopAnimating()
  120. }
  121. func setActivityIndicator(color: UIColor) {
  122. activityIndicator = UIActivityIndicatorView.init(style: .whiteLarge)
  123. activityIndicator?.hidesWhenStopped = true
  124. activityIndicator?.startAnimating()
  125. activityIndicator?.color = color
  126. }
  127. }
  128. //MARK: -
  129. extension IndefiniteAnimatedView {
  130. override func willMove(toSuperview newSuperview: UIView?) {
  131. if let _ = newSuperview {
  132. layoutAnimatedLayer()
  133. } else {
  134. getIndefinteAnimatedLayer().removeFromSuperlayer()
  135. indefinteAnimatedLayer = nil
  136. }
  137. }
  138. private func layoutAnimatedLayer() {
  139. let calayer = getIndefinteAnimatedLayer()
  140. self.layer.addSublayer(calayer)
  141. let widthDiff: CGFloat = bounds.width - layer.bounds.width
  142. let heightDiff: CGFloat = bounds.height - layer.bounds.height
  143. let xPos = bounds.width - layer.bounds.width / 2 - widthDiff / 2
  144. let yPos = bounds.height - layer.bounds.height / 2 - heightDiff / 2
  145. calayer.position = CGPoint.init(x: xPos, y: yPos)
  146. }
  147. override func sizeThatFits(_ size: CGSize) -> CGSize {
  148. let localRadius : CGFloat = radius ?? 18
  149. let localStrokeThickness : CGFloat = strokeThickness ?? 2
  150. for view in self.subviews {
  151. if let _ = view as? UIActivityIndicatorView {
  152. return CGSize.init(width: 50, height: 50)
  153. }
  154. }
  155. return CGSize.init(width: (localRadius + localStrokeThickness / 2 + 5) * 2, height: (localRadius + localStrokeThickness / 2 + 5) * 2)
  156. }
  157. private func loadImageBundle(named imageName:String) -> UIImage? {
  158. #if SWIFT_PACKAGE
  159. var imageBundle = Bundle.init(for: IHProgressHUD.self)
  160. if let resourcePath = Bundle.module.path(forResource: "IHProgressHUD", ofType: "bundle") {
  161. if let resourcesBundle = Bundle(path: resourcePath) {
  162. imageBundle = resourcesBundle
  163. }
  164. }
  165. return UIImage(named: imageName, in: imageBundle, compatibleWith: nil)
  166. #else
  167. var imageBundle = Bundle.init(for: IHProgressHUD.self)
  168. if let resourcePath = imageBundle.path(forResource: "IHProgressHUD", ofType: "bundle") {
  169. if let resourcesBundle = Bundle(path: resourcePath) {
  170. imageBundle = resourcesBundle
  171. }
  172. }
  173. return (UIImage(named: imageName, in: imageBundle, compatibleWith: nil))
  174. #endif
  175. }
  176. }