⌛️A customizable animated gradient loading bar.

Overview

GradientLoadingBar

Swift5.0 CI Status Code Coverage Version License Platform

A customizable animated gradient loading bar. Inspired by iOS 7 Progress Bar from Codepen.

Example

Example

To run the example project, clone the repo, and open the workspace from the Example directory.

Requirements

  • Swift 5.0
  • Xcode 10.2+
  • iOS 9.0+

Integration

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate GradientLoadingBar into your Xcode project using CocoaPods, specify it in your Podfile:

pod 'GradientLoadingBar', '~> 2.0'
Carthage

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

github "fxm90/GradientLoadingBar" ~> 2.0

Run carthage update to build the framework and drag the built GradientLoadingBar.framework, as well as the dependency LightweightObservable.framework, into your Xcode project.

Swift Package Manager

The Swift Package Manager is a tool for automating the distribution of Swift code and is integrated into the swift compiler. It is in early development, but Gradient Loading Bar does support its use on supported platforms.

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

dependencies: [
    .package(url: "https://github.com/fxm90/GradientLoadingBar", from: "2.0.3")
]

How to use

This framework provides three classes:

  • GradientLoadingBar: A controller, managing the visibility of the GradientActivityIndicatorView on the current key window.
  • NotchGradientLoadingBar: A subclass of GradientLoadingBar, wrapping the GradientActivityIndicatorView around the notch of the iPhone.
  • GradientActivityIndicatorView: A UIView containing the gradient with the animation. It can be added as a subview to another view either inside the interface builder or programmatically. Both ways are shown inside the example application.

GradientLoadingBar

To get started, import the module GradientLoadingBar into your file and save an instance of GradientLoadingBar() on a property of your view-controller. To show the loading bar, simply call the fadeIn(duration:completion) method and after your async operations have finished call the fadeOut(duration:completion) method.

class UserViewController: UIViewController {

    private let gradientLoadingBar = GradientLoadingBar()

    // ...

    override func viewDidLoad() {
        super.viewDidLoad()

        gradientLoadingBar.fadeIn()

        userService.loadUserData { [weak self] _ in
            // ...
            // Be sure to call this on the main thread!
            self?.gradientLoadingBar.fadeOut()
        }
    }
}
Configuration

You can overwrite the default configuration by calling the initializers with the optional parameters height and isRelativeToSafeArea:

let gradientLoadingBar = GradientLoadingBar(
    height: 4.0,
    isRelativeToSafeArea: true
)
– Parameter height: CGFloat

By setting this parameter you can set the height for the loading bar (defaults to 3.0)

– Parameter isRelativeToSafeArea: Bool

With this parameter you can configure, whether the loading bar should be positioned relative to the safe area (defaults to true).

Example with isRelativeToSafeArea set to true. Example

Example with isRelativeToSafeArea set to false. Example

Note

There is a third option which will wrap the loading bar around the iPhone notch. See documentation of the class NotchGradientLoadingBar for further details.

Properties
gradientColors: [UIColor]

This property adjusts the gradient colors shown on the loading bar.

progressAnimationDuration: TimeInterval

This property adjusts the duration of the animation moving the gradient from left to right.

Methods
fadeIn(duration:completion)

This method fades-in the loading bar. You can adjust the duration with corresponding parameter. Furthermore you can pass in a completion handler that gets called once the animation is finished.

fadeOut(duration:completion)

This methods fades-out the loading bar. You can adjust the duration with corresponding parameter. Furthermore you can pass in a completion handler that gets called once the animation is finished.

Custom shared instance (Singleton)

If you need the loading bar on multiple / different parts of your app, you can use the given static shared variable:

GradientLoadingBar.shared.fadeIn()

// Do e.g. server calls etc.

GradientLoadingBar.shared.fadeOut()

If you wish to customize the shared instance, you can add the following code e.g. to your app delegate didFinishLaunchingWithOptions method and overwrite the shared variable:

GradientLoadingBar.shared = GradientLoadingBar(height: 5.0)

NotchGradientLoadingBar

This subclass of the GradientLoadingBar will wrap the loading bar around the notch of the iPhone.

For iPhones without a safe area, the behaviour stays the same as mentioned in the above documentation of the GradientLoadingBar.

let notchGradientLoadingBar = NotchGradientLoadingBar(
    height: 3.0
)

Example

GradientActivityIndicatorView

In case you don't want to add the loading bar onto the key-window, this framework provides the GradientActivityIndicatorView, which is a direct subclass of UIView. You can add the view to another view either inside the interface builder or programmatically.

E.g. View added as a subview to a UINavigationBar. Example

E.g. View added as a subview to a UIButton. Example

Note

The progress-animation starts and stops according to the isHidden flag. Setting this flag to false will start the animation, setting this to true will stop the animation. Often you don't want to directly show / hide the view and instead smoothly fade it in or out. Therefore the view provides the methods fadeIn(duration:completion) and fadeOut(duration:completion). Based on my gist, these methods adjust the alpha value of the view and update the isHidden flag accordingly.

Properties
gradientColors: [UIColor]

This property adjusts the gradient colors shown on the loading bar.

progressAnimationDuration: TimeInterval

This property adjusts the duration of the animation moving the gradient from left to right.

To see all these screenshots in a real app, please have a look at the example application. For further customization you can also subclass GradientLoadingBar and overwrite the method setupConstraints().

Troubleshooting

Interface Builder Support

Unfortunatly the Interface Builder support is currently broken for Cocoapods frameworks. If you need Interface Builder support, add the following code to your Podfile and run pod install again. Afterwards you should be able to use the GradientLoadingBar inside the Interface Builder :)

  post_install do |installer|
    installer.pods_project.build_configurations.each do |config|
      next unless config.name == 'Debug'

      config.build_settings['LD_RUNPATH_SEARCH_PATHS'] = [
        '$(FRAMEWORK_SEARCH_PATHS)'
      ]
    end
  end

Source: Cocoapods – Issue 7606

Author

Felix Mau (me(@)felix.hamburg)

License

GradientLoadingBar is available under the MIT license. See the LICENSE file for more info.

Comments
  • Made extension methods internal to avoid conflicts with other libraries

    Made extension methods internal to avoid conflicts with other libraries

    Public UIView extension methods fadeIn(duration:, completion:) and fadeOut(duration:, completion:) were conflicting with other libraries (e.g. SwifterSwift) own fadeIn(duration:, completion:) and fadeOut(duration:, completion:) causing the compiler to raise ambiguous use of ... type errors. Since the goal for this library is to provide a great loading bar (which it does masterfully) and not new UIView functionality, its extension methods should be internal.

    opened by emilrb 9
  • iPhone X Compatibility

    iPhone X Compatibility

    Hello, I would like to use this awesome library but I could not figure it how to solve iPhone X Layout compatibility. Can you share your solution if it is already working with iPhone X layout ? Thank you :)

    Type: Enhancement 
    opened by WrathChaos 9
  • iPad Pro 12.9 hasNotch is true

    iPad Pro 12.9 hasNotch is true

    Hi, There is a problem with the hasNotch check, because the iPad Pro 12.9-inch has safeAreaInsets 20 https://developer.apple.com/forums/thread/110724 Thanks.

    Type: Bug 
    opened by prisacariugeorge 5
  • missing return in a function expected to return 'string'

    missing return in a function expected to return 'string'

    My project uses GradientLoadingBar and I've just ran pod update and got the version bump to 2.2.1.

    However, when building with Xcode 13, an error is generated:

    Pods/GradientLoadingBar/GradientLoadingBar/Classes/ViewModel/NotchGradientLoadingBarViewModel.swift:81:5: 
    error: missing return in a function expected to return 'String'
    

    Had to revert back to 2.1.1 unfortunately...

    Type: Bug Priority: High 
    opened by rubnov 4
  • FEATURE: Loader position

    FEATURE: Loader position

    Hey, I think it would be awesome to be able to reposition the loader. I think it is achievable by making the setupConstraints(keyWindow: UIWindow){} function public, something like this:

    public func setupConstraints(keyWindow: UIWindow, leadingAnchor: NSLayoutConstraint, trailingAnchor: NSLayoutConstraint, topAnchor: NSLayoutConstraint, heightAnchor: NSLayoutConstraint) {
            gradientView.leadingAnchor.constraint(equalTo: keyWindow.leadingAnchor).isActive = true
            gradientView.trailingAnchor.constraint(equalTo: keyWindow.trailingAnchor).isActive = true
    
            gradientView.topAnchor.constraint(equalTo: keyWindow.topAnchor).isActive = true
            gradientView.heightAnchor.constraint(equalToConstant: CGFloat(height)).isActive = true
        }
    

    Another solution would be to add another variables in DefaultValues struct e.g public static let leadingAnchor: NSLayoutConstraint!

    Type: Enhancement 
    opened by MaeseppTarvo 4
  • Struct 'DefaultValues' is private and cannot be referenced from a default argument value

    Struct 'DefaultValues' is private and cannot be referenced from a default argument value

    Hey, in the GradientLoadingBar class there is the following error:

    Struct 'DefaultValues' is private and cannot be referenced from a default argument value

    I am using Xcode 9 and the 1.1.4 version of your library.

    Type: Bug Status: Completed 
    opened by MaeseppTarvo 4
  • Draw a shape for specified height instead of 1 line

    Draw a shape for specified height instead of 1 line

    I found a problem. When you increase the loading bar height, the notch corners don't align properly. So instead of drawing just one line of given width at half distance from top, draw a full shape starting at "x:0, y:0", travel device width, go down "height" -1 pixels, come back and up to "x:0, y:0".

    In order to see the problem more clearly you can set the line height to 20 Before After .

    opened by alinfarcas12 3
  • Not showing on UINavigationController

    Not showing on UINavigationController

    I cannot seem to get the gradient loading bar to show onto the top of a UINavigationController. I've looked at the previous issue, but despite what I set the onView to (including leaving as default) the bar is not shown.

    opened by george-gymamigo 2
  • Observable Extension missing argument for parameter #2 in call

    Observable Extension missing argument for parameter #2 in call

    Hi,

    Thanks for the great library. Currently using 1.1.12 and Observable 1.4 which was updated 2 days ago. The file Observable + ObserveDistinct.swift cannot be compiled with the error message 'Missing argument for parameter #2 in call'. Any workarounds for now until the next update? Thanks.

    Environment: Xcode 10 Swift version: Swift 4.2

    2018-11-14 7 30 10
    opened by chienplee 2
  • Enhance BezierPath of NotchGradientLoadingBar

    Enhance BezierPath of NotchGradientLoadingBar

    When drawing the circles for the NotchGradientLoadingBar on the "way back" we use the same center point as in the beginning but adapt the radius.

    This produces visually better ("rounder") results:

    image image

    See: https://twitter.com/lilykonings/status/1567317037126680576?s=46&t=Cm2Q8BsqSY_nbCrZqGE08g

    Type: Enhancement 
    opened by fxm90 1
  • Support of Swift Package Manager

    Support of Swift Package Manager

    Adds Package.swift file which enables usage with SPM. Note, that the most correct usage with SPM will be possible only when there is new release that includes Package.swift. Until then, dependency can be added by branch or commit hash which is not allowed in published packages.

    opened by TwoDollarsEsq 1
Owner
Felix M.
iOS & Frontend Developer 📲 https://twitter.com/_fxm90 🐦 https://codepen.io/fxm90 🖥 https://instagram.com/fxm90 📸
Felix M.
UIProgressView replacement with an highly and fully customizable animated progress bar in pure Core Graphics

The YLProgressBar is an UIProgressView replacement with a highly and fully customizable animated progress bar in pure Core Graphics. It has been imple

Yannick Loriot 1.3k Jan 5, 2023
A simple animated progress bar in Swift

DSGradientProgressView Introduction DSGradientProgressView is a simple and customizable animated progress bar written in Swift. Inspired by GradientPr

Dhol Studio 445 Oct 13, 2022
Simple and powerful animated progress bar with dots

Example To run the example project, clone the repo, and run pod install from the Example directory first. Requirements iOS 8.0+ Swift 3.0+ Installatio

Nikola Corlija 42 Dec 5, 2022
A number of preset loading indicators created with SwiftUI

A number of preset loading indicators created with SwiftUI

Exyte 968 Jan 8, 2023
NVActivityIndicatorView is a collection of awesome loading animations.

NVActivityIndicatorView is a collection of awesome loading animations.

Vinh Nguyen 10.3k Jan 5, 2023
A metaball loading written in Swift.

DBMetaballLoading Synopsis A metaball loading written in Swift. Special thanks to dodola's MetaballLoading, which is an android project. The animation

ChildhoodAndy 72 Jul 2, 2022
A lightweight and awesome loading Activity Indicator for your iOS app.

BPCircleActivityIndicator BPCircleActivityIndicator is a clean and easy-to-use Activity Indicator meant to display the progress of an ongoing task on

Ben.Park 46 Aug 12, 2022
Awesome loading animations using 3D engine written with Swift

RSLoadingView Introduction RSLoadingView bring your app to the new age of loading animations using 3D engine. Written with Swift Customizable Using Ap

null 419 Dec 16, 2022
A simple and awesome loading Activity Indicator(with block moving animation) for your iOS app.

BPBlockActivityIndicator BPBlockActivityIndicator is a clean and easy-to-use Activity Indicator meant to display the progress of an ongoing task on iO

Ben.Park 43 Nov 6, 2021
Windless makes it easy to implement invisible layout loading view.

Windless Windless makes it easy to implement invisible layout loading view. Contents Requirements Installation Usage Looks Credits Communication Licen

ArLupin 940 Dec 22, 2022
💀 An easy way to create sliding CAGradientLayer animations! Works great for creating skeleton screens for loading content.

Skeleton is an easy way to create sliding CAGradientLayer animations! It works great for creating skeleton screens: ??‍?? Usage The entire library com

Gonzalo Nuñez 668 Nov 2, 2022
Show pleasant loading view for your users 😍

RHPlaceholder ?? Because traditional loading view like UIActivityIndicatorView or similar one are no longer so trendy (Facebook or Instagram apps are

Robert Herdzik 238 Nov 2, 2022
Flexible Stepped Progress Bar for IOS

FlexibleSteppedProgressBar This is a stepped progress bar for IOS. The base code is derived from ABSteppedProgressBar. Most of the design is customisa

Amrata Baghel 549 Jan 6, 2023
StatusBarOverlay will automatically show a "No Internet Connection" bar when your app loses connection, and hide it again

StatusBarOverlay will automatically show a "No Internet Connection" bar when your app loses connection, and hide it again. It supports apps which hide the status bar and The Notch

Idle Hands Apps 160 Nov 2, 2022
💈 Retro looking progress bar straight from the 90s

Description Do you miss the 90s? We know you do. Dial-up internet, flickering screens, brightly colored websites and, of course, this annoyingly slow

HyperRedink 18 Nov 24, 2022
Material Linear Progress Bar for your iOS apps

LinearProgressBar Material Linear Progress Bar for your iOS apps Installation Carthage: github "Recouse/LinearProgressBar" CocoaPods: Add this to you

Firdavs Khaydarov 161 Dec 5, 2022
Completely customizable progress based loaders drawn using custom CGPaths written in Swift

FillableLoaders Completely customizable progress based loaders drawn using custom CGPaths written in Swift Waves Plain Spike Rounded Demo: Changelog:

Pol Quintana 2.1k Dec 31, 2022
⌛️A customizable animated gradient loading bar.

GradientLoadingBar A customizable animated gradient loading bar. Inspired by iOS 7 Progress Bar from Codepen. Example To run the example project, clon

Felix M. 791 Dec 26, 2022
Animated Mask Label is a nice gradient animated label.

Animated Mask Label Demo Screen Screenshot Demo/Example For demo: $ pod try AnimatedMaskLabel To run the example project, clone the repo, and run pod

Jogendra 19 Dec 13, 2022
📊 A customizable gradient progress bar (UIProgressView).

GradientProgressBar A customizable gradient progress bar (UIProgressView). Inspired by iOS 7 Progress Bar from Codepen. Example To run the example pro

Felix M. 490 Dec 16, 2022