Logo

Pod Version SPM compatible Carthage compatible License Twitter: @hukicamer

AHDownloadButton 是一款可定制的下载按钮,类似于 Apple App Store 应用最新版本(自 iOS 11 起)中的下载按钮。它具有下载进度动画以及下载状态之间的动画过渡:开始下载、等待中、下载中和已下载。 您可以在我的博客上找到有关实现的更多详细信息

要求

用法

代码

要在代码中使用 AHDownloadButton,您只需创建一个新实例,并将其作为子视图添加到您想要的视图中。

  let downloadButton = AHDownloadButton()
  downloadButton.frame = CGRect(origin: origin, size: size)
  view.addSubview(downloadButton)

该按钮可以有 4 种不同的状态

可以通过其 state 属性更改按钮的状态。

代理

您可以使用 AHDownloadButtonDelegate 来监控按钮上的点击事件,并在需要时更新按钮的状态。要更新当前的下载进度,请使用 progress 属性。 这是一个如何实现的示例

extension DownloadViewController: AHDownloadButtonDelegate {

    func downloadButton(_ downloadButton: AHDownloadButton, tappedWithState state: AHDownloadButton.State)
        switch state {
        case .startDownload:

            // set the download progress to 0
            downloadButton.progress = 0

            // change state to pending and wait for download to start
            downloadButton.state = .pending

            // initiate download and update state to .downloading
            startDownloadingFile()

        case .pending:

            // button tapped while in pending state
            break

        case .downloading:

            // button tapped while in downloading state - stop downloading
            downloadButton.progress = 0
            downloadButton.state = .startDownload

        case .downloaded:

            // file is downloaded and can be opened
            openDownloadedFile()

        }
    }
}

您还可以使用闭包而不是 AHDownloadButtonDelegate,方法是设置 didTapDownloadButtonActiondownloadButtonStateChangedAction 属性。

定制

AHDownloadButton 可以被定制。以下是可以用来定制按钮的属性

  1. 使用自定义初始化器 init(alignment: HorizontalAlignment) 来设置水平对齐属性。 HorizontalAlignment 决定了等待中和下载中圆圈的位置。 位置可以是 centerleftright。 默认值为 center

  2. 当按钮处于 startDownload 状态时的自定义属性

  1. 当按钮处于 pending 状态时的自定义属性
  1. 当按钮处于 downloading 状态时的自定义属性
  1. 当按钮处于 downloaded 状态时的自定义属性
  1. transitionAnimationDuration - 按钮不同状态之间的动画持续时间

特别说明

AHDownloadButtonstartDownloaddownloaded 状态下会根据按钮标题计算其宽度。 使用 startDownloadButtonTitleSidePaddingdownloadedButtonTitleSidePadding 属性来定制按钮处于上述状态时的宽度。

示例

要运行示例项目,请克隆 repo,并首先从 Example 目录运行 pod install

安装

CocoaPods

CocoaPods 是 Cocoa 项目的依赖管理工具。 您可以使用以下命令安装它

$ gem install cocoapods

要使用 CocoaPods 将 AHDownloadButton 集成到您的 Xcode 项目中,请在您的 Podfile 中指定它

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'AHDownloadButton'
end

然后,运行以下命令

$ pod install

Carthage

Carthage 是一个分散的依赖管理工具,它构建您的依赖项并为您提供二进制框架。 要使用 Carthage 将 AHDownloadButton 集成到您的 Xcode 项目中,请在您的 Cartfile 中指定它

github "amerhukic/AHDownloadButton" ~> 1.3.0

Swift Package Manager

Swift Package Manager 是一种用于自动化 Swift 代码分发的工具,并已集成到 swift 编译器中。

设置好 Swift 包后,将 AHDownloadButton 添加为依赖项就像将其添加到 Package.swiftdependencies 值一样简单。

dependencies: [
    .package(url: "https://github.com/amerhukic/AHDownloadButton", .upToNextMajor(from: "1.3.0"))
]

作者

Amer Hukić

许可

AHDownloadButton 在 MIT 许可下获得许可。 有关详细信息,请查看 LICENSE 文件。