From 2126287e9c7fd21a008b61ca814a9e5393bc3696 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20B=C3=B8dskov?= Date: Thu, 11 Apr 2019 15:22:37 +0200 Subject: [PATCH 1/2] adds potential button image again after spinner is dismissed --- Spinner/Spinner.swift | 31 ++++++++++++------- .../SpinnerDemo/Base.lproj/Main.storyboard | 23 +++++++++++--- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/Spinner/Spinner.swift b/Spinner/Spinner.swift index 92dc503..38b4a7d 100644 --- a/Spinner/Spinner.swift +++ b/Spinner/Spinner.swift @@ -29,6 +29,7 @@ public class SpinnerView: NSObject, Spinner { fileprivate var controlTitleColors: [ControlTitleColor]? fileprivate var controlTitleAttributes: [ControlTitleAttributes]? + fileprivate var buttonImageView: UIImageView? fileprivate var activityIndicator: UIActivityIndicatorView? fileprivate var imageView: UIImageView? fileprivate var userInteractionEnabledAtReception = true @@ -54,10 +55,13 @@ public class SpinnerView: NSObject, Spinner { let spinnerView = SpinnerView() spinnerView.userInteractionEnabledAtReception = view.isUserInteractionEnabled + if let button = view as? UIButton { + spinnerView.buttonImageView = button.imageView + } //In case the previous spinner wasn't dismissed if let oldSpinner = view.subviews.filter({ $0 is Spinner }).first as? Spinner { - + spinnerView.userInteractionEnabledAtReception = true //We will need to assume userinteraction should be restored as we do not have access to the original SpinnerView oldSpinner.dismiss() } @@ -100,7 +104,6 @@ public class SpinnerView: NSObject, Spinner { - Returns: A reference to the Spinner that was created, so that it can be dismissed as needed. */ public static func showSpinner(inButton button: UIButton, style: UIActivityIndicatorView.Style = .white, color:UIColor? = nil, disablesUserInteraction:Bool = true) -> SpinnerView { - let spinnerView = showSpinner(inView: button, style: style, color: color) spinnerView.controlTitleColors = button.allTitleColors() button.removeAllTitleColors() @@ -124,13 +127,13 @@ public class SpinnerView: NSObject, Spinner { if let superView = activityIndicator?.superview { superView.isUserInteractionEnabled = self.userInteractionEnabledAtReception if let button = superView as? UIButton { - button.restore(titleColors: controlTitleColors, attributedStrings: controlTitleAttributes) + button.restore(titleColors: controlTitleColors, attributedStrings: controlTitleAttributes, buttonImageView: buttonImageView) } } else if let superView = imageView?.superview { superView.isUserInteractionEnabled = self.userInteractionEnabledAtReception if let button = superView as? UIButton { - button.restore(titleColors: controlTitleColors, attributedStrings: controlTitleAttributes) + button.restore(titleColors: controlTitleColors, attributedStrings: controlTitleAttributes, buttonImageView: buttonImageView) } } @@ -156,7 +159,7 @@ public extension SpinnerView { - Parameter images: An array containing the UIImages to use for the animation. - Parameter duration: The animation duration. */ - public static func set(customImages images: [UIImage], duration: TimeInterval) { + static func set(customImages images: [UIImage], duration: TimeInterval) { let image = UIImageView(frame: CGRect(x: 0, y: 0, width: images[0].size.width, height: images[0].size.height)) image.animationImages = images image.animationDuration = duration @@ -175,7 +178,7 @@ public extension SpinnerView { - Returns: A reference to the `Spinner` that was created, so that it can be dismissed as needed. */ - public static func showCustomSpinner(inView view: UIView, dimBackground: Bool = false) -> SpinnerView { + static func showCustomSpinner(inView view: UIView, dimBackground: Bool = false) -> SpinnerView { if let image = animationImage { //In case the previous spinner wasn't dismissed @@ -217,7 +220,7 @@ public extension SpinnerView { - Returns: A reference to the ActivityIndicator that was created, so that it can be dismissed as needed */ - public static func showCustomSpinner(inButton button: UIButton, disablesUserInteraction:Bool = true) -> SpinnerView { + static func showCustomSpinner(inButton button: UIButton, disablesUserInteraction:Bool = true) -> SpinnerView { let spinnerView = showCustomSpinner(inView: button) button.isUserInteractionEnabled = !disablesUserInteraction spinnerView.controlTitleColors = button.allTitleColors() @@ -255,7 +258,7 @@ extension UIImageView: Spinner { fileprivate extension UIButton { // Extension to return an array of every color for every button state - fileprivate func allTitleColors() -> [ControlTitleColor] { + func allTitleColors() -> [ControlTitleColor] { var colors: [ControlTitleColor] = [ (UIControl.State(), titleColor(for: UIControl.State())), (.highlighted, titleColor(for: .highlighted)), @@ -272,7 +275,7 @@ fileprivate extension UIButton { return colors } - fileprivate func allTitleAttributes() -> [ControlTitleAttributes] { + func allTitleAttributes() -> [ControlTitleAttributes] { var attributes: [ControlTitleAttributes] = [ (UIControl.State(), attributedTitle(for: UIControl.State())), (.highlighted, attributedTitle(for: .highlighted)), @@ -294,7 +297,7 @@ fileprivate extension UIButton { - Parameter colors: An array of ControlTitleColor each containing a UIControlState and a UIColor */ - fileprivate func restore(titleColors colors: [ControlTitleColor]?, attributedStrings: [ControlTitleAttributes]?) { + func restore(titleColors colors: [ControlTitleColor]?, attributedStrings: [ControlTitleAttributes]?, buttonImageView: UIImageView?) { if colors != nil { for color in colors! { if titleColor(for: color.0) == .clear { @@ -308,19 +311,23 @@ fileprivate extension UIButton { self.setAttributedTitle(string.1, for: string.0) } } + + if let buttonImageView = buttonImageView { + addSubview(buttonImageView) + } } /** Sets all the the buttons title colors to clear */ - fileprivate func removeAllTitleColors() { + func removeAllTitleColors() { let clearedColors = allTitleColors().map({ return ($0.0, UIColor.clear) }) for color in clearedColors { setTitleColor(color.1, for: color.0) } } - fileprivate func removeAllAttributedStrings() { + func removeAllAttributedStrings() { self.setAttributedTitle(nil, for: .normal) self.setAttributedTitle(nil, for: .highlighted) self.setAttributedTitle(nil, for: .disabled) diff --git a/SpinnerDemo/SpinnerDemo/SpinnerDemo/Base.lproj/Main.storyboard b/SpinnerDemo/SpinnerDemo/SpinnerDemo/Base.lproj/Main.storyboard index 99ce6c6..b9a0b03 100644 --- a/SpinnerDemo/SpinnerDemo/SpinnerDemo/Base.lproj/Main.storyboard +++ b/SpinnerDemo/SpinnerDemo/SpinnerDemo/Base.lproj/Main.storyboard @@ -1,9 +1,11 @@ - + + + + - - + @@ -16,10 +18,11 @@ - + +