Skip to content
This repository was archived by the owner on Mar 10, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions Spinner/Spinner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class SpinnerView: NSObject, Spinner {
fileprivate var spinner: UIActivityIndicatorView?
fileprivate var imageView: UIImageView?
fileprivate var userInteractionEnabledAtReception = true
fileprivate var dimView: UIView?

/**
To display the indicator centered in a view.
Expand All @@ -40,11 +41,12 @@ public class SpinnerView: NSObject, Spinner {
- Parameter style: A constant that specifies the style of the object to be created.
- Parameter color: A UIColor that specifies the tint of the spinner
- Parameter disablesUserInteraction: A boolean that specifies if the user interaction on the view should be disabled while the spinner is shown. Default is true
- Parameter dimBackground: A Boolean specifying if background should be dimmed while showing spinner. Default is false

- Returns: A reference to the Spinner that was created, so that it can be dismissed as needed.
*/

public static func showSpinner(inView view: UIView, style: UIActivityIndicatorViewStyle = .white, color:UIColor? = nil, disablesUserInteraction: Bool = true) -> SpinnerView {
public static func showSpinner(inView view: UIView, style: UIActivityIndicatorViewStyle = .white, color:UIColor? = nil, disablesUserInteraction: Bool = true, dimBackground: Bool = false) -> SpinnerView {
let center = CGPoint(x: view.bounds.size.width/2, y: view.bounds.size.height/2)

let spinner = UIActivityIndicatorView(activityIndicatorStyle: style)
Expand All @@ -54,16 +56,23 @@ public class SpinnerView: NSObject, Spinner {

if disablesUserInteraction {
spinner.frame = view.frame
// This should not modify the layer background color
// spinner.layer.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.3).cgColor
view.isUserInteractionEnabled = false
}

spinner.color = color
spinner.center = center
spinner.autoresizingMask = [.flexibleTopMargin, .flexibleLeftMargin, .flexibleRightMargin, .flexibleBottomMargin]

spinner.startAnimating()
view.addSubview(spinner)

if dimBackground {
spinnerView.dimView = UIView(frame: view.bounds)
if let dimView = spinnerView.dimView {
dimView.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.3)
view.insertSubview(dimView, belowSubview: spinner)
}
}
spinnerView.spinner = spinner

return spinnerView
Expand Down Expand Up @@ -112,6 +121,7 @@ public class SpinnerView: NSObject, Spinner {

spinner?.dismiss()
imageView?.dismiss()
dimView?.removeFromSuperview()
}
}

Expand Down Expand Up @@ -146,20 +156,29 @@ public extension SpinnerView {
a custom `UIImageView`.

- Parameter view: The view to display the indicator in.
- Parameter dimBackground: A Boolean specifying if background should be dimmed while showing spinner. Default is false

- Returns: A reference to the `Spinner` that was created, so that it can be dismissed as needed.
*/
public static func showCustomSpinner(inView view: UIView) -> SpinnerView {
public static func showCustomSpinner(inView view: UIView, dimBackground: Bool = false) -> SpinnerView {
if let image = animationImage {
let imageView = UIImageView(frame: view.bounds)
imageView.contentMode = .scaleAspectFit
imageView.contentMode = .center
imageView.animationDuration = image.animationDuration
imageView.animationImages = image.animationImages
imageView.startAnimating()
view.addSubview(imageView)

let spinnerView = SpinnerView()
spinnerView.imageView = imageView
if dimBackground {
spinnerView.dimView = UIView(frame: view.bounds)
if let dimView = spinnerView.dimView {
dimView.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.3)
view.insertSubview(dimView, belowSubview: imageView)
}
}

return spinnerView
}
else {
Expand Down
17 changes: 17 additions & 0 deletions SpinnerTests/SpinnerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,23 @@ class SpinnerTests: XCTestCase {
XCTAssertTrue(view.isUserInteractionEnabled)
}

func testShowSpinnerInViewDimBackground() {
view.isUserInteractionEnabled = true
_ = SpinnerView.showSpinner(inView: view, dimBackground: true)
let hasSpinner = view.subviews.contains {$0 is Spinner}
let dimmedBackground = view.subviews.count == 2
XCTAssert(hasSpinner && dimmedBackground && !view.isUserInteractionEnabled)
}

func testDismissSpinnerInViewDimBackground() {
let spinner = SpinnerView.showSpinner(inView: view, dimBackground: true)
spinner.dismiss()
let hasSpinner = view.subviews.contains {$0 is Spinner}
XCTAssertFalse(hasSpinner)
XCTAssertTrue(view.isUserInteractionEnabled)
XCTAssert(view.subviews.count == 0)
}

func testShowCustomSpinnerInView() {
view.isUserInteractionEnabled = true
_ = SpinnerView.showCustomSpinner(inView: view)
Expand Down