Skip to content

Commit

Permalink
Add gifs and README
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Ho committed Aug 16, 2019
1 parent 5b6fa39 commit 4044d20
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 16 deletions.
46 changes: 33 additions & 13 deletions LoadingButtons/LoadingButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,17 @@ open class LoadingButton: UIButton {
Set to true to add shadow to the button.
*/
open var withShadow: Bool = false
/**
The corner radius of the button
*/
open var cornerRadius: CGFloat = 12.0 {
didSet {
self.layer.cornerRadius = self.cornerRadius
}
}
/**
Shadow view.
*/
open var shadowLayer: UIView?
/**
Get all views in the button. Views include the button itself and the shadow.
Expand Down Expand Up @@ -69,17 +75,17 @@ open class LoadingButton: UIButton {
withShadow: Bool = false
) {
super.init(frame: frame)

// Set the icon of the button
if let icon = icon {
self.setImage(icon)
}

// Set the title of the button
if let text = text {
self.setTitle(text)
self.setTitleColor(textColor, for: .normal)
self.titleLabel?.adjustsFontSizeToFitWidth = true
}

// Set button contents
self.titleLabel?.font = font
self.bgColor = bgColor
self.backgroundColor = bgColor
Expand All @@ -89,7 +95,7 @@ open class LoadingButton: UIButton {
self.withShadow = withShadow
self.cornerRadius = cornerRadius
}

// draw
open override func draw(_ rect: CGRect) {
super.draw(rect)
if shadowAdded || !withShadow { return }
Expand All @@ -100,17 +106,22 @@ open class LoadingButton: UIButton {
shadowLayer.setAsShadow(bounds: bounds, cornerRadius: self.cornerRadius)
self.superview?.insertSubview(shadowLayer, belowSubview: self)
}

// Required init
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}

/**
Display the loader inside the button.

- Parameter userInteraction: Enable the user interaction while displaying the loader.
- Parameter completion: The completion handler.
*/
open func showLoader(userInteraction: Bool, _ completion: LBCallback = nil) {
showLoader([titleLabel, imageView], userInteraction: userInteraction, completion)
}

private func showLoader(_ viewsToBeHide: [UIView?], userInteraction: Bool = false, _ completion: LBCallback = nil) {
guard self.subviews.contains(indicator) == false else { return }
guard !self.subviews.contains(indicator) else { return }
// Set up loading indicator
indicator.radius = min(0.7*self.frame.height/2, indicator.radius)
isLoading = true
Expand All @@ -132,11 +143,17 @@ open class LoadingButton: UIButton {
}
/**
Show a loader inside the button with image.

- Parameter userInteraction: Enable user interaction while showing the loader.
*/
open func showLoaderWithImage(userInteraction: Bool = false) {
showLoader([self.titleLabel], userInteraction: userInteraction)
}

/**
Hide the loader displayed.

- Parameter completion: The completion handler.
*/
open func hideLoader(_ completion: LBCallback = nil) {
guard self.subviews.contains(indicator) else { return }
isLoading = false
Expand All @@ -150,32 +167,35 @@ open class LoadingButton: UIButton {
completion?()
}
}

/**
Make the content of the button fill the button.
*/
public func fillContent() {
self.contentVerticalAlignment = .fill
self.contentHorizontalAlignment = .fill
}

// layoutSubviews
open override func layoutSubviews() {
super.layoutSubviews()
indicator.center = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2)
}
// MARK: Touch
// touchesBegan
open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
self.backgroundColor = self.bgColor.getColorTint()
}

// touchesEnded
open override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
self.backgroundColor = self.bgColor
}

// touchesCancelled
open override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesCancelled(touches, with: event)
self.backgroundColor = self.bgColor
}

// touchesMoved
open override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesMoved(touches, with: event)
self.backgroundColor = self.bgColor.getColorTint()
Expand Down
6 changes: 5 additions & 1 deletion LoadingButtonsDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ class ViewController: UIViewController {
.lineScalePulse, .ballBeat
]
private var selectedCell: UICollectionViewCell?
// MARK: - Package-protected properties
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
// viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
Expand Down Expand Up @@ -52,7 +56,7 @@ class ViewController: UIViewController {
sender.hideLoader()
} else {
sender.showLoader(userInteraction: true)
delay(5.0) {
delay(3.0) {
sender.hideLoader()
}
}
Expand Down
65 changes: 63 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,77 @@
## Loading Buttons
A collection of loading buttons and their styling written in Swift.


[![Generic badge](https://img.shields.io/badge/Swift-5.0-orange.svg)](https://shields.io/) [![Generic badge](https://img.shields.io/badge/iOS-11.0+-blue.svg)](https://shields.io/) [![Generic badge](https://img.shields.io/badge/Version-0.0.1.beta-orange.svg)](https://shields.io/) [![Generic badge](https://img.shields.io/badge/platform-ios-green.svg)](https://shields.io/)

You may see the following [Medium](https://medium.com/) article for detailed explanation of creating loading buttons.

- [Create Loading Buttons in iOS using Swift](https://medium.com/@twho/create-loading-buttons-in-ios-using-swift-63ec77eebda?sk=8f69e9a7760cabacde096c34cc416f95)

## Key Features
-
- The example gives you **8** choices of loading indicators with the loading button.
- The **IndicatorProtocol** clearly defines the functions and properties. You can refer to it and customize your own.
- The **LoadingButton** class is made to be **open**, from which you can easily inherit and create your own.

## Requirements
- Swift 5.0
- iOS 11.0+

## Usage
### Declaration
```swift
// You may set the frame to zero if you use AutoLayout to handle the frame. Otherwise, specify the frame in initializer.
btnLoading = LoadingButton(frame: .zero, text: "Button", textColor: .black, bgColor: .white)
```
### System Default
```swift
btnLoading.indicator = UIActivityIndicatorView()
```
<img src="gif/sysdefault.gif" alt="System Default" width="350"/>

### Material Design
```swift
btnLoading.indicator = MaterialLoadingIndicator(color: .gray)
```
<img src="gif/materialdesign.gif" alt="Material Design" width="350"/>

### Ball Pulse
```swift
btnLoading.indicator = BallPulseSyncIndicator(color: .gray)
```
<img src="gif/ballpulse.gif" alt="Ball Pulse" width="350"/>

### Ball Pulse Sync
```swift
btnLoading.indicator = BallSpinFadeIndicator(color: .gray)
```
<img src="gif/ballpulsesync.gif" alt="Ball Pulse Sync" width="350"/>

### Ball Spin
```swift
btnLoading.indicator = LineScalePulseIndicator(color: .gray)
```
<img src="gif/ballspin.gif" alt="Ball Spin" width="350"/>

### Line Scale
```swift
btnLoading.indicator = LineScaleIndicator(color: .gray)
```
<img src="gif/linescale.gif" alt="Line Scale" width="350"/>

### Line Scale Pulse
```swift
btnLoading.indicator = BallPulseIndicator(color: .gray)
```
<img src="gif/linescalepulse.gif" alt="Line Scale Pulse" width="350"/>

### Ball Beat
```swift
btnLoading.indicator = BallBeatIndicator(color: .gray)
```
<img src="gif/ballbeat.gif" alt="Ball Beat" width="350"/>

## Credits
* [Material Design](https://material.io/design/)
* [Material Design Widgets Lite](https://github.com/twho/material-design-widgets-lite-ios)
* [Le Van Nghia](https://github.com/sharad-paghadal/MaterialKit/tree/master/Source)
* [ninjaprox](https://github.com/ninjaprox/NVActivityIndicatorView)
Binary file added gif/ballbeat.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gif/ballpulse.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gif/ballpulsesync.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gif/ballspin.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gif/linescale.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gif/linescalepulse.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gif/materialdesign.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gif/sysdefault.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4044d20

Please sign in to comment.