加载 1.1.2

Loading 1.1.2

LEE 维护。



Loading 1.1.2

  • 作者:
  • LEE

Loading

Swift

一个用 Swift 编写的优雅加载视图

天朝子民

特性

  • 快速添加加载视图。
  • 不需要继承。
  • 轻量级扩展。
  • 良好的可扩展性。
  • 无代码侵入。

安装

CocoaPods - Podfile

source 'https://github.com/lixiang1994/Specs'

pod 'Loading'

Carthage - Cartfile

github "lixiang1994/Loading"

使用

首先确保导入框架

import Loading

以下是一些使用示例。所有设备也可作为模拟器使用

扩展

查看

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.loading.start(.system(.white))

button.loading.stop()

自定义标签

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)

加载视图

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()

指示器

  • 系统
view.loading.start(.system(.gray))
  • 旋转
view.loading.start(.rotate(image))
  • 圆形
view.loading.start(.circle(line: .white, line: 3.0))
  • 图片
view.loading.start(.images([image]))

重新加载器

// custom text 
LoadingReloader.text("Unable to load, please click again")

// custom image
LoadingReloader.image(image)

// custom view
LoadingReloader.view(view)

自定义

例如:

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()
    }
}

更多例子可以参考演示。

贡献

如果您需要实现特定功能或有遇到bug,请创建问题。如果您扩展了Loading自行使用并希望其他人也能使用,请提交拉取请求。

许可证

Loading遵从MIT许可证。更多信息请参阅LICENSE文件。