Skip to content
This repository has been archived by the owner on Mar 10, 2022. It is now read-only.

Commit

Permalink
Merge pull request #41 from nodes-ios/develop
Browse files Browse the repository at this point in the history
adds potential button image again after spinner is dismissed
  • Loading branch information
anho authored Apr 11, 2019
2 parents ae94aec + 8dd6958 commit e9f95b9
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 25 deletions.
7 changes: 5 additions & 2 deletions Spinner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0800;
LastUpgradeCheck = 1010;
LastUpgradeCheck = 1020;
ORGANIZATIONNAME = Nodes;
TargetAttributes = {
275BCA281C57C50A00FF3647 = {
Expand All @@ -271,10 +271,11 @@
};
buildConfigurationList = 275BCA231C57C50A00FF3647 /* Build configuration list for PBXProject "Spinner" */;
compatibilityVersion = "Xcode 3.2";
developmentRegion = English;
developmentRegion = en;
hasScannedForEncodings = 0;
knownRegions = (
en,
Base,
);
mainGroup = 275BCA1F1C57C50A00FF3647;
productRefGroup = 275BCA2A1C57C50A00FF3647 /* Products */;
Expand Down Expand Up @@ -373,6 +374,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
Expand Down Expand Up @@ -431,6 +433,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1010"
LastUpgradeVersion = "1020"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 1 addition & 1 deletion Spinner.xcodeproj/xcshareddata/xcschemes/Spinner.xcscheme
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1010"
LastUpgradeVersion = "1020"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
31 changes: 19 additions & 12 deletions Spinner/Spinner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
}
Expand Down Expand Up @@ -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()
Expand All @@ -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)
}
}

Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)),
Expand All @@ -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)),
Expand All @@ -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 {
Expand All @@ -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)
Expand Down
Loading

0 comments on commit e9f95b9

Please sign in to comment.