Loading
                                
                                
                                
                                    Loading copied to clipboard
                            
                            
                            
                        An elegant loading view written in swift
Loading
An elegant loading view written in swift
天朝子民
Features
- [x] Quickly add a loading view.
 - [x] No inheritance required.
 - [x] Lightweight expansion.
 - [x] Good scalability.
 - [x] No code intrusion.
 
Installation
CocoaPods - Podfile
source 'https://github.com/lixiang1994/Specs'
pod 'Loading'
Carthage - Cartfile
github "lixiang1994/Loading"
Usage
First make sure to import the framework:
import Loading
Here are some usage examples. All devices are also available as simulators:
Extension
View
view.loading.start(
    .rotate(#imageLiteral(resourceName: "loading"), at: 30),
    .text("again", font: .systemFont(ofSize: 13), color: #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0))
)
view.loading.fail() { 
    // reloader click action
}
view.loading.stop()
Button
button.loading.start(.system(.white))
button.loading.stop()
Custom Tag
view.loading.start(
    .rotate(#imageLiteral(resourceName: "loading"), at: 30),
    .text("again", font: .systemFont(ofSize: 13), color: #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)),
    tag: 12345
)
view.loading.fail(12345) { 
    // reloader click action
}
view.loading.stop(12345)
LoadingView
let loadingView = Loading.view(.system(.gray))
loadingView.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
view.addSubview(loadingView)
loadingView.action { 
    // click loading view action
}
loadingView.reloader.action {
    // click reloader action
}
// Start loading
loadingView.start()
// Stop loading
loadingView.stop()
// Failed to load
loadingView.fail()
Indicators
- system
 
view.loading.start(.system(.gray))
- rotate
 
view.loading.start(.rotate(image))
- circle
 
view.loading.start(.circle(line: .white, line: 3.0))
- images
 
view.loading.start(.images([image]))
Reloaders
// custom text 
LoadingReloader.text("Unable to load, please click again")
// custom image
LoadingReloader.image(image)
// custom view
LoadingReloader.view(view)
Custom
e.g.
class LoadingXXXXXXIndicator: LoadingIndicator {
    /* ... */
    
    public override func start() {
        /* ... */
    }
    
    public override func stop() {
        /* ... */
    }
}
class LoadingXXXXXXReloader: LoadingReloader {
    /* ... */
    
    @objc 
    private func buttonAction(_ sender: UIButton) {
        action?()
    }
}
class LoadingXXXXXStateView<Indicator: LoadingIndicator, Reloader: LoadingReloader>: LoadingStateView<Indicator, Reloader> {
    
    required init(_ indicator: Indicator, _ reloader: Reloader) {
        super.init(indicator, reloader)
        setup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    private func setup() {
        isHidden = true
        
        addSubview(indicator)
        addSubview(reloader)
    }
    
    override public func layoutSubviews() {
        super.layoutSubviews()
        
        do {
            let offset = indicator.offset
            let x = bounds.width * 0.5 + offset.x
            let y = bounds.height * 0.5 + offset.y
            indicator.center = CGPoint(x: x, y: y)
        }
        do {
            let offset = reloader.offset
            let x = bounds.width * 0.5 + offset.x
            let y = bounds.height * 0.5 + offset.y
            reloader.center = CGPoint(x: x, y: y)
        }
    }
    
    public override func start() {
        isHidden = false
        reloader.isHidden = true
        indicator.isHidden = false
        indicator.start()
    }
    
    public override func stop() {
        isHidden = true
        reloader.isHidden = true
        indicator.isHidden = true
        indicator.stop()
    }
    
    public override func fail() {
        isHidden = false
        reloader.isHidden = false
        indicator.isHidden = true
        indicator.stop()
    }
}
More examples can refer to the demo.
Contributing
If you have the need for a specific feature that you want implemented or if you experienced a bug, please open an issue. If you extended the functionality of Loading yourself and want others to use it too, please submit a pull request.
License
Loading is under MIT license. See the LICENSE file for more info.