ProductPromotion
Logo

Swift

made by https://0x3d.site

GitHub - amerhukic/AHDownloadButton: Customizable download button with progress and transition animations. It is based on Apple's App Store download button.
Customizable download button with progress and transition animations. It is based on Apple's App Store download button. - amerhukic/AHDownloadButton
Visit Site

GitHub - amerhukic/AHDownloadButton: Customizable download button with progress and transition animations. It is based on Apple's App Store download button.

GitHub - amerhukic/AHDownloadButton: Customizable download button with progress and transition animations. It is based on Apple's App Store download button.

AHDownloadButton is a customizable download button similar to the download button in the latest version of Apple's App Store app (since iOS 11). It features download progress animation as well as animated transitions between download states: start download, pending, downloading and downloaded. You can find more details about the implementation on my blog.

Requirements

  • iOS 8.0+
  • Xcode 10.2+
  • Swift 5.0+

Usage

Code

To use AHDownloadButton in code, you simply create a new instance and add it as a subview to your desired view:

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

The button can have 4 different states:

  • startDownload - initial state before downloading
  • pending - state for preparing for download
  • downloading - state when the user is downloading
  • downloaded - state when the user finished downloading

The state of the button can be changed through its state property.

Delegate

You can use the AHDownloadButtonDelegate to monitor taps on the button and update button's state if needed. To update the current download progress, use the progress property. Here is an example how it could be implemented:

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

        }
    }
}

You can also use closures instead of the AHDownloadButtonDelegate by setting the didTapDownloadButtonAction and downloadButtonStateChangedAction properties.

Customisation

AHDownloadButton can be customized. These are the properties that can be used for customizing the button:

  1. Use the custom initializer init(alignment: HorizontalAlignment) to set the horizontal alignment property. HorizontalAlignment determines the position of the pending and downloading circles. The position can either be center , left or right. The default value is center.

  2. Customization properties when button is in startDownload state:

  • startDownloadButtonTitle - button's title
  • startDownloadButtonTitleFont - button's title font
  • startDownloadButtonTitleSidePadding - padding for left and right side of button's title
  • startDownloadButtonHighlightedBackgroundColor - background color for the button when it's in highlighted state (when the user presses the button)
  • startDownloadButtonNonhighlightedBackgroundColor - background color for the button when it's in nonhighlighted state (when the button is not pressed)
  • startDownloadButtonHighlightedTitleColor - title color for the button when it's in highlighted state (when the user presses the button)
  • startDownloadButtonNonhighlightedTitleColor - title color for the button when it's in nonhighlighted state (when the button is not pressed)
  1. Customization properties when button is in pending state:
  • pendingCircleColor - color of the pending circle
  • pendingCircleLineWidth - width of the pending circle
  1. Customization properties when button is in downloading state:
  • downloadingButtonHighlightedTrackCircleColor - color for the track circle when it's in highlighted state (when the user presses the button)
  • downloadingButtonNonhighlightedTrackCircleColor - color for the track circle when it's in nonhighlighted state (when the button is not pressed)
  • downloadingButtonHighlightedProgressCircleColor - color for the progress circle when it's in highlighted state (when the user presses the button)
  • downloadingButtonNonhighlightedProgressCircleColor - color for the progress circle when it's in nonhighlighted state (when the button is not pressed)
  • downloadingButtonHighlightedStopViewColor - color for the stop view in the middle of the progress circle when it's in highlighted state (when the user presses the button)
  • downloadingButtonNonhighlightedStopViewColor - color for the stop view in the middle of the progress circle when it's in nonhighlighted state (when the button is not pressed)
  • downloadingButtonCircleLineWidth - width of the downloading circle
  1. Customization properties when button is in downloaded state:
  • downloadedButtonTitle - button's title
  • downloadedButtonTitleFont - button's title font
  • downloadedButtonTitleSidePadding - padding for left and right side of button's title
  • downloadedButtonHighlightedBackgroundColor - background color for the button when it's in highlighted state (when the user presses the button)
  • downloadedButtonNonhighlightedBackgroundColor - background color for the button when it's in nonhighlighted state (when the button is not pressed)
  • downloadedButtonHighlightedTitleColor - title color for the button when it's in highlighted state (when the user presses the button)
  • downloadedButtonNonhighlightedTitleColor - title color for the button when it's in nonhighlighted state (when the button is not pressed)
  1. transitionAnimationDuration - animation duration between the different states of the button

Special note

AHDownloadButton in startDownload and downloaded states calculates its width based on button title. Use the startDownloadButtonTitleSidePadding and downloadedButtonTitleSidePadding properties to customise the width when the button is in the aforementioned states.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate AHDownloadButton into your Xcode project using CocoaPods, specify it in your Podfile:

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

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

Then, run the following command:

$ pod install

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To integrate AHDownloadButton into your Xcode project using Carthage, specify it in your Cartfile:

github "amerhukic/AHDownloadButton" ~> 1.3.0

Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler.

Once you have your Swift package set up, adding AHDownloadButton as a dependency is as easy as adding it to the dependencies value of your Package.swift.

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

Author

Amer Hukić

License

AHDownloadButton is licensed under the MIT license. Check the LICENSE file for details.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory