Giter Site home page Giter Site logo

loading's Introduction

Loading

Swift

An elegant loading view written in swift

Features

  • Quickly add a loading view.
  • No inheritance required.
  • Lightweight expansion.
  • Good scalability.
  • 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 {
    /* ... */
    override func action(_ handle: @escaping (() -> Void)) {
        /* ... */
    }
}
class LoadingXXXXXView: LoadingView {
    
    private var action: (()->Void)?
    
    required init(_ indicator: LoadingIndicator, _ reloader: LoadingReloader) {
        super.init(indicator, reloader)
        setup()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    private func setup() {
        isHidden = true
        
        addSubview(indicator)
        addSubview(reloader)
        
        let tap = UITapGestureRecognizer(
            target: self,
            action: #selector(tapAction)
        )
        addGestureRecognizer(tap)
    }
    
    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)
        }
    }
    
    @objc private func tapAction(_ sender: UITapGestureRecognizer) {
        action?()
    }
    
    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()
    }
    
    public override func action(_ handle: @escaping (()->Void)) {
        action = handle
    }
}

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.