⌛️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.
Customizable HUD for swift

HUD Customizable HUD. Requirements Installation Swift Package Manager The Swift Package Manager is a tool for automating the distribution of Swift cod

Horizontal Systems 1 Oct 6, 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
📊 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
Simple utility to change macOS Big Sur menu bar color by appending a solid color or gradient rectangle to a wallpaper image

Change menu bar color in macOS Big Sur Simple utility to change macOS Big Sur menu bar color by appending a solid color or gradient rectangle to a wal

Igor Kulman 876 Jan 5, 2023
🌈 Highly customizable Core Graphics based gradient view for iOS

MKGradientView Highly customizable Core Graphics based gradient view Features Available gradient types: Linear (Axial) Radial (Circular) Conical (Angu

Max Konovalov 167 Dec 27, 2022
Easy customizable avatar image asynchronously with progress bar animated

JDSwiftAvatarProgress ##Objective-C JDAvatarProgress is available in Objective-C also, JDAvatarProgress Usage To run the example project, clone the re

Jelly Development 86 May 16, 2022
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 SwiftUI view for dynamically rendering content based upon "loading", "error", and "completed" data loading states.

SwiftUIAsyncContentView A SwiftUI view for dynamically rendering content based upon "loading", "error", and "completed" data loading states.. Installa

CypherPoet 0 Dec 26, 2021
Beautiful animated placeholders for showing loading of data

KALoader Create breautiful animated placeholders for showing loading of data. You can change colors like you want. Swift 4 compatible. Usage To add an

Kirill Avery 105 May 2, 2022
A small and flexible (well documented) UIButton subclass with animated loading progress, and completion animation.

ButtonProgressBar-iOS Example For LIVE PREVIEW on Appetize in your browser itself, click here. To run the example project, clone the repo, and run pod

Pushkar Sharma 566 Dec 9, 2022
ListPlaceholder is a swift library allows you to easily add facebook style animated loading placeholder to your tableviews or collection views.

ListPlaceholder ListPlaceholder Facebook news feed style animation Features ListPlaceholder is a swift library allows you to easily add facebook style

Moayad Al Kouz 628 Dec 19, 2022
🎨 Gradient animation effect like Instagram

Pastel ?? Gradient animation effect like Instagram Example override func viewDidLoad() { super.viewDidLoad() let pastelView = PastelView(fram

Cruz 3.4k Jan 1, 2023
UIGradient - A simple and powerful library for using gradient layer, image, color

UIGradient is now available on CocoaPods. Simply add the following to your project Podfile, and you'll be good to go.

Đinh Quang Hiếu 247 Dec 1, 2022
Color framework for Swift & Objective-C (Gradient colors, hexcode support, colors from images & more).

Swift 3 To use the Swift 3 version, add this to your Podfile (until 2.2 or higher is released): pod 'ChameleonFramework/Swift', :git => 'https://githu

Vicc Alexander 12.5k Dec 27, 2022
Conical (angular) gradient for iOS written in Swift

AEConicalGradient Conical (angular) gradient in Swift I hope that somebody will find this useful. And nice. Usage AEConicalGradient is a minion which

Marko Tadić 82 Dec 27, 2022
A powerful and easy to use live mesh gradient renderer for iOS.

MeshKit A powerful and easy to use live mesh gradient renderer for iOS. This project wouldn't be possible without the awesome work from Moving Parts a

Ethan Lipnik 51 Jan 1, 2023
Conical (angular) gradient for iOS written in Swift

AEConicalGradient Conical (angular) gradient in Swift I hope that somebody will find this useful. And nice. Usage AEConicalGradient is a minion which

Marko Tadić 82 Dec 27, 2022
🎞 Powerful gradient animations made simple for iOS.

AnimatedGradientView is a UIView subclass which makes it simple to add animated gradients to your iOS app. It is written purely in Swift. Further docu

Ross Butler 431 Dec 12, 2022