Преглед изворни кода

Add size strategy to Indicator

onevcat пре 6 година
родитељ
комит
f7c63de26a
2 измењених фајлова са 27 додато и 0 уклоњено
  1. 11 0
      Sources/Extensions/ImageView+Kingfisher.swift
  2. 16 0
      Sources/Views/Indicator.swift

+ 11 - 0
Sources/Extensions/ImageView+Kingfisher.swift

@@ -356,6 +356,17 @@ extension KingfisherWrapper where Base: KFCrossPlatformImageView {
                     equalTo: base.centerXAnchor, constant: newIndicator.centerOffset.x).isActive = true
                 view.centerYAnchor.constraint(
                     equalTo: base.centerYAnchor, constant: newIndicator.centerOffset.y).isActive = true
+
+                switch newIndicator.sizeStrategy(in: base) {
+                case .intrinsicSize:
+                    break
+                case .full:
+                    view.heightAnchor.constraint(equalTo: base.heightAnchor, constant: 0).isActive = true
+                    view.widthAnchor.constraint(equalTo: base.widthAnchor, constant: 0).isActive = true
+                case .size(let size):
+                    view.heightAnchor.constraint(equalToConstant: size.height).isActive = true
+                    view.widthAnchor.constraint(equalToConstant: size.width).isActive = true
+                }
                 
                 newIndicator.view.isHidden = true
             }

+ 16 - 0
Sources/Views/Indicator.swift

@@ -67,6 +67,14 @@ public protocol Indicator {
     
     /// The indicator view which would be added to the super view.
     var view: IndicatorView { get }
+
+    func sizeStrategy(in imageView: KFCrossPlatformImageView) -> IndicatorSizeStrategy
+}
+
+public enum IndicatorSizeStrategy {
+    case intrinsicSize
+    case full
+    case size(CGSize)
 }
 
 extension Indicator {
@@ -74,6 +82,10 @@ extension Indicator {
     /// Default implementation of `centerOffset` of `Indicator`. The default value is `.zero`, means that there is
     /// no offset for the indicator view.
     public var centerOffset: CGPoint { return .zero }
+
+    public func sizeStrategy(in imageView: KFCrossPlatformImageView) -> IndicatorSizeStrategy {
+        return .full
+    }
 }
 
 // Displays a NSProgressIndicator / UIActivityIndicatorView
@@ -114,6 +126,10 @@ final class ActivityIndicator: Indicator {
         }
     }
 
+    func sizeStrategy(in imageView: KFCrossPlatformImageView) -> IndicatorSizeStrategy {
+        return .intrinsicSize
+    }
+
     init() {
         #if os(macOS)
             activityIndicatorView = NSProgressIndicator(frame: CGRect(x: 0, y: 0, width: 16, height: 16))