You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

218 lines
11 KiB

5 years ago
  1. # SVProgressHUD
  2. ![Pod Version](https://img.shields.io/cocoapods/v/SVProgressHUD.svg?style=flat)
  3. ![Pod Platform](https://img.shields.io/cocoapods/p/SVProgressHUD.svg?style=flat)
  4. ![Pod License](https://img.shields.io/cocoapods/l/SVProgressHUD.svg?style=flat)
  5. [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-green.svg?style=flat)](https://github.com/Carthage/Carthage)
  6. [![CocoaPods compatible](https://img.shields.io/badge/CocoaPods-compatible-green.svg?style=flat)](https://cocoapods.org)
  7. `SVProgressHUD` is a clean and easy-to-use HUD meant to display the progress of an ongoing task on iOS and tvOS.
  8. ![SVProgressHUD](http://f.cl.ly/items/2G1F1Z0M0k0h2U3V1p39/SVProgressHUD.gif)
  9. ## Demo
  10. Try `SVProgressHUD` on [Appetize.io](https://appetize.io/app/p8r2cvy8kq74x7q7tjqf5gyatr).
  11. ## Installation
  12. ### From CocoaPods
  13. [CocoaPods](http://cocoapods.org) is a dependency manager for Objective-C, which automates and simplifies the process of using 3rd-party libraries like `SVProgressHUD` in your projects. First, add the following line to your [Podfile](http://guides.cocoapods.org/using/using-cocoapods.html):
  14. ```ruby
  15. pod 'SVProgressHUD'
  16. ```
  17. If you want to use the latest features of `SVProgressHUD` use normal external source dependencies.
  18. ```ruby
  19. pod 'SVProgressHUD', :git => 'https://github.com/SVProgressHUD/SVProgressHUD.git'
  20. ```
  21. This pulls from the `master` branch directly.
  22. Second, install `SVProgressHUD` into your project:
  23. ```ruby
  24. pod install
  25. ```
  26. ### Carthage
  27. [Carthage](https://github.com/Carthage/Carthage) is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To integrate `SVProgressHUD` into your Xcode project using Carthage, specify it in your `Cartfile`:
  28. ```ogdl
  29. github "SVProgressHUD/SVProgressHUD"
  30. ```
  31. Run `carthage bootstrap` to build the framework in your repository's Carthage directory. You can then include it in your target's `carthage copy-frameworks` build phase. For more information on this, please see [Carthage's documentation](https://github.com/carthage/carthage#if-youre-building-for-ios-tvos-or-watchos).
  32. ### Manually
  33. * Drag the `SVProgressHUD/SVProgressHUD` folder into your project.
  34. * Take care that `SVProgressHUD.bundle` is added to `Targets->Build Phases->Copy Bundle Resources`.
  35. * Add the **QuartzCore** framework to your project.
  36. ## Swift
  37. Even though `SVProgressHUD` is written in Objective-C, it can be used in Swift with no hassle. If you use [CocoaPods](http://cocoapods.org) add the following line to your [Podfile](http://guides.cocoapods.org/using/using-cocoapods.html):
  38. ```ruby
  39. use_frameworks!
  40. ```
  41. If you added `SVProgressHUD` manually, just add a [bridging header](https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html) file to your project with the `SVProgressHUD` header included.
  42. ## Usage
  43. (see sample Xcode project in `/Demo`)
  44. `SVProgressHUD` is created as a singleton (i.e. it doesn't need to be explicitly allocated and instantiated; you directly call `[SVProgressHUD method]`).
  45. **Use `SVProgressHUD` wisely! Only use it if you absolutely need to perform a task before taking the user forward. Bad use case examples: pull to refresh, infinite scrolling, sending message.**
  46. Using `SVProgressHUD` in your app will usually look as simple as this (using Grand Central Dispatch):
  47. ```objective-c
  48. [SVProgressHUD show];
  49. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  50. // time-consuming task
  51. dispatch_async(dispatch_get_main_queue(), ^{
  52. [SVProgressHUD dismiss];
  53. });
  54. });
  55. ```
  56. ### Showing the HUD
  57. You can show the status of indeterminate tasks using one of the following:
  58. ```objective-c
  59. + (void)show;
  60. + (void)showWithStatus:(NSString*)string;
  61. ```
  62. If you'd like the HUD to reflect the progress of a task, use one of these:
  63. ```objective-c
  64. + (void)showProgress:(CGFloat)progress;
  65. + (void)showProgress:(CGFloat)progress status:(NSString*)status;
  66. ```
  67. ### Dismissing the HUD
  68. The HUD can be dismissed using:
  69. ```objective-c
  70. + (void)dismiss;
  71. + (void)dismissWithDelay:(NSTimeInterval)delay;
  72. ```
  73. If you'd like to stack HUDs, you can balance out every show call using:
  74. ```
  75. + (void)popActivity;
  76. ```
  77. The HUD will get dismissed once the popActivity calls will match the number of show calls.
  78. Or show a confirmation glyph before before getting dismissed a little bit later. The display time depends on `minimumDismissTimeInterval` and the length of the given string.
  79. ```objective-c
  80. + (void)showInfoWithStatus:(NSString*)string;
  81. + (void)showSuccessWithStatus:(NSString*)string;
  82. + (void)showErrorWithStatus:(NSString*)string;
  83. + (void)showImage:(UIImage*)image status:(NSString*)string;
  84. ```
  85. ## Customization
  86. `SVProgressHUD` can be customized via the following methods:
  87. ```objective-c
  88. + (void)setDefaultStyle:(SVProgressHUDStyle)style; // default is SVProgressHUDStyleLight
  89. + (void)setDefaultMaskType:(SVProgressHUDMaskType)maskType; // default is SVProgressHUDMaskTypeNone
  90. + (void)setDefaultAnimationType:(SVProgressHUDAnimationType)type; // default is SVProgressHUDAnimationTypeFlat
  91. + (void)setContainerView:(UIView*)containerView; // default is window level
  92. + (void)setMinimumSize:(CGSize)minimumSize; // default is CGSizeZero, can be used to avoid resizing
  93. + (void)setRingThickness:(CGFloat)width; // default is 2 pt
  94. + (void)setRingRadius:(CGFloat)radius; // default is 18 pt
  95. + (void)setRingNoTextRadius:(CGFloat)radius; // default is 24 pt
  96. + (void)setCornerRadius:(CGFloat)cornerRadius; // default is 14 pt
  97. + (void)setBorderColor:(nonnull UIColor*)color; // default is nil
  98. + (void)setBorderWidth:(CGFloat)width; // default is 0
  99. + (void)setFont:(UIFont*)font; // default is [UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline]
  100. + (void)setForegroundColor:(UIColor*)color; // default is [UIColor blackColor], only used for SVProgressHUDStyleCustom
  101. + (void)setBackgroundColor:(UIColor*)color; // default is [UIColor whiteColor], only used for SVProgressHUDStyleCustom
  102. + (void)setBackgroundLayerColor:(UIColor*)color; // default is [UIColor colorWithWhite:0 alpha:0.4], only used for SVProgressHUDMaskTypeCustom
  103. + (void)setImageViewSize:(CGSize)size; // default is 28x28 pt
  104. + (void)setShouldTintImages:(BOOL)shouldTintImages; // default is YES
  105. + (void)setInfoImage:(UIImage*)image; // default is the bundled info image provided by Freepik
  106. + (void)setSuccessImage:(UIImage*)image; // default is bundled success image from Freepik
  107. + (void)setErrorImage:(UIImage*)image; // default is bundled error image from Freepik
  108. + (void)setViewForExtension:(UIView*)view; // default is nil, only used if #define SV_APP_EXTENSIONS is set
  109. + (void)setGraceTimeInterval:(NSTimeInterval)interval; // default is 0 seconds
  110. + (void)setMinimumDismissTimeInterval:(NSTimeInterval)interval; // default is 5.0 seconds
  111. + (void)setMaximumDismissTimeInterval:(NSTimeInterval)interval; // default is CGFLOAT_MAX
  112. + (void)setFadeInAnimationDuration:(NSTimeInterval)duration; // default is 0.15 seconds
  113. + (void)setFadeOutAnimationDuration:(NSTimeInterval)duration; // default is 0.15 seconds
  114. + (void)setMaxSupportedWindowLevel:(UIWindowLevel)windowLevel; // default is UIWindowLevelNormal
  115. + (void)setHapticsEnabled:(BOOL)hapticsEnabled; // default is NO
  116. ```
  117. Additionally `SVProgressHUD` supports the `UIAppearance` protocol for most of the above methods.
  118. ### Hint
  119. As standard `SVProgressHUD` offers two preconfigured styles:
  120. * `SVProgressHUDStyleLight`: White background with black spinner and text
  121. * `SVProgressHUDStyleDark`: Black background with white spinner and text
  122. If you want to use custom colors use `setForegroundColor` and `setBackgroundColor:`. These implicitly set the HUD's style to `SVProgressHUDStyleCustom`.
  123. ## Haptic Feedback
  124. For users with newer devices (starting with the iPhone 7), `SVProgressHUD` can automatically trigger haptic feedback depending on which HUD is being displayed. The feedback maps as follows:
  125. * `showSuccessWithStatus:` <-> `UINotificationFeedbackTypeSuccess`
  126. * `showInfoWithStatus:` <-> `UINotificationFeedbackTypeWarning`
  127. * `showErrorWithStatus:` <-> `UINotificationFeedbackTypeError`
  128. To enable this functionality, use `setHapticsEnabled:`.
  129. Users with devices prior to iPhone 7 will have no change in functionality.
  130. ## Notifications
  131. `SVProgressHUD` posts four notifications via `NSNotificationCenter` in response to being shown/dismissed:
  132. * `SVProgressHUDWillAppearNotification` when the show animation starts
  133. * `SVProgressHUDDidAppearNotification` when the show animation completes
  134. * `SVProgressHUDWillDisappearNotification` when the dismiss animation starts
  135. * `SVProgressHUDDidDisappearNotification` when the dismiss animation completes
  136. Each notification passes a `userInfo` dictionary holding the HUD's status string (if any), retrievable via `SVProgressHUDStatusUserInfoKey`.
  137. `SVProgressHUD` also posts `SVProgressHUDDidReceiveTouchEventNotification` when users touch on the overall screen or `SVProgressHUDDidTouchDownInsideNotification` when a user touches on the HUD directly. For this notifications `userInfo` is not passed but the object parameter contains the `UIEvent` that related to the touch.
  138. ## App Extensions
  139. When using `SVProgressHUD` in an App Extension, `#define SV_APP_EXTENSIONS` to avoid using unavailable APIs. Additionally call `setViewForExtension:` from your extensions view controller with `self.view`.
  140. ## Contributing to this project
  141. If you have feature requests or bug reports, feel free to help out by sending pull requests or by [creating new issues](https://github.com/SVProgressHUD/SVProgressHUD/issues/new). Please take a moment to
  142. review the guidelines written by [Nicolas Gallagher](https://github.com/necolas):
  143. * [Bug reports](https://github.com/necolas/issue-guidelines/blob/master/CONTRIBUTING.md#bugs)
  144. * [Feature requests](https://github.com/necolas/issue-guidelines/blob/master/CONTRIBUTING.md#features)
  145. * [Pull requests](https://github.com/necolas/issue-guidelines/blob/master/CONTRIBUTING.md#pull-requests)
  146. ## License
  147. `SVProgressHUD` is distributed under the terms and conditions of the [MIT license](https://github.com/SVProgressHUD/SVProgressHUD/blob/master/LICENSE.txt). The success, error and info icons are made by [Freepik](http://www.freepik.com) from [Flaticon](http://www.flaticon.com) and are licensed under [Creative Commons BY 3.0](http://creativecommons.org/licenses/by/3.0/).
  148. ## Credits
  149. `SVProgressHUD` is brought to you by [Sam Vermette](http://samvermette.com), [Tobias Tiemerding](http://tiemerding.com) and [contributors to the project](https://github.com/SVProgressHUD/SVProgressHUD/contributors). If you're using `SVProgressHUD` in your project, attribution would be very appreciated.