Indicator.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // Indicator.swift
  3. // Kingfisher
  4. //
  5. // Created by João D. Moreira on 30/08/16.
  6. //
  7. // Copyright (c) 2016 Wei Wang <onevcat@gmail.com>
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. #if os(OSX)
  27. import AppKit
  28. #else
  29. import UIKit
  30. #endif
  31. #if os(OSX)
  32. public typealias IndicatorView = NSView
  33. #else
  34. public typealias IndicatorView = UIView
  35. #endif
  36. // MARK: - Indicator Protocol
  37. public protocol Indicator {
  38. func startAnimatingView()
  39. func stopAnimatingView()
  40. var viewCenter: CGPoint { get set }
  41. var view: IndicatorView { get }
  42. }
  43. extension Indicator {
  44. #if os(OSX)
  45. var viewCenter: CGPoint {
  46. get {
  47. let frame = view.frame
  48. return CGPoint(x: frame.origin.x + frame.size.width / 2.0, y: frame.origin.y + frame.size.height / 2.0 )
  49. }
  50. set {
  51. let frame = view.frame
  52. let newFrame = CGRect(x: newValue.x - frame.size.width / 2.0,
  53. y: newValue.y - frame.size.height / 2.0,
  54. width: frame.size.width,
  55. height: frame.size.height)
  56. view.frame = newFrame
  57. }
  58. }
  59. #else
  60. var viewCenter: CGPoint {
  61. get {
  62. return view.center
  63. }
  64. set {
  65. view.center = newValue
  66. }
  67. }
  68. #endif
  69. }
  70. // MARK: - ActivityIndicator
  71. // Displays a NSProgressIndicator / UIActivityIndicatorView
  72. struct ActivityIndicator: Indicator {
  73. #if os(OSX)
  74. private let activityIndicatorView: NSProgressIndicator
  75. #else
  76. private let activityIndicatorView: UIActivityIndicatorView
  77. #endif
  78. var view: IndicatorView {
  79. return activityIndicatorView
  80. }
  81. func startAnimatingView() {
  82. #if os(OSX)
  83. activityIndicatorView.startAnimation(nil)
  84. #else
  85. activityIndicatorView.startAnimating()
  86. #endif
  87. activityIndicatorView.hidden = false
  88. }
  89. func stopAnimatingView() {
  90. #if os(OSX)
  91. activityIndicatorView.stopAnimation(nil)
  92. #else
  93. activityIndicatorView.stopAnimating()
  94. #endif
  95. activityIndicatorView.hidden = true
  96. }
  97. init() {
  98. #if os(OSX)
  99. activityIndicatorView = NSProgressIndicator(frame: CGRect(x: 0, y: 0, width: 16, height: 16))
  100. #if swift(>=2.3)
  101. activityIndicatorView.controlSize = .Small
  102. #else
  103. activityIndicatorView.controlSize = .SmallControlSize
  104. #endif
  105. activityIndicatorView.style = .SpinningStyle
  106. #else
  107. #if os(tvOS)
  108. let indicatorStyle = UIActivityIndicatorViewStyle.White
  109. #else
  110. let indicatorStyle = UIActivityIndicatorViewStyle.Gray
  111. #endif
  112. activityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle:indicatorStyle)
  113. activityIndicatorView.autoresizingMask = [.FlexibleLeftMargin, .FlexibleRightMargin, .FlexibleBottomMargin, .FlexibleTopMargin]
  114. #endif
  115. }
  116. }
  117. // MARK: - ImageIndicator
  118. // Displays an ImageView. Supports gif
  119. struct ImageIndicator: Indicator {
  120. private let animatedImageIndicatorView: ImageView
  121. var view: IndicatorView {
  122. return animatedImageIndicatorView
  123. }
  124. init(imageData data: NSData) {
  125. let image = Image.kf_imageWithData(data, scale: 1.0, preloadAllGIFData: true)
  126. animatedImageIndicatorView = ImageView()
  127. animatedImageIndicatorView.image = image
  128. #if os(OSX)
  129. // Need for gif to animate on OSX
  130. self.animatedImageIndicatorView.imageScaling = .ScaleNone
  131. self.animatedImageIndicatorView.canDrawSubviewsIntoLayer = true
  132. #else
  133. animatedImageIndicatorView.contentMode = .Center
  134. animatedImageIndicatorView.autoresizingMask = [.FlexibleLeftMargin,
  135. .FlexibleRightMargin,
  136. .FlexibleBottomMargin,
  137. .FlexibleTopMargin]
  138. #endif
  139. }
  140. func startAnimatingView() {
  141. #if os(OSX)
  142. animatedImageIndicatorView.animates = true
  143. #else
  144. animatedImageIndicatorView.startAnimating()
  145. #endif
  146. animatedImageIndicatorView.hidden = false
  147. }
  148. func stopAnimatingView() {
  149. #if os(OSX)
  150. animatedImageIndicatorView.animates = false
  151. #else
  152. animatedImageIndicatorView.stopAnimating()
  153. #endif
  154. animatedImageIndicatorView.hidden = true
  155. }
  156. }