MBProgressHUD.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. //
  2. // MBProgressHUD.h
  3. // Version 1.2.0
  4. // Created by Matej Bukovinski on 2.4.09.
  5. //
  6. // This code is distributed under the terms and conditions of the MIT license.
  7. // Copyright © 2009-2016 Matej Bukovinski
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. #import <Foundation/Foundation.h>
  27. #import <UIKit/UIKit.h>
  28. #import <CoreGraphics/CoreGraphics.h>
  29. @class MBBackgroundView;
  30. @protocol MBProgressHUDDelegate;
  31. extern CGFloat const MBProgressMaxOffset;
  32. typedef NS_ENUM(NSInteger, MBProgressHUDMode) {
  33. /// UIActivityIndicatorView.
  34. MBProgressHUDModeIndeterminate,
  35. /// A round, pie-chart like, progress view.
  36. MBProgressHUDModeDeterminate,
  37. /// Horizontal progress bar.
  38. MBProgressHUDModeDeterminateHorizontalBar,
  39. /// Ring-shaped progress view.
  40. MBProgressHUDModeAnnularDeterminate,
  41. /// Shows a custom view.
  42. MBProgressHUDModeCustomView,
  43. /// Shows only labels.
  44. MBProgressHUDModeText
  45. };
  46. typedef NS_ENUM(NSInteger, MBProgressHUDAnimation) {
  47. /// Opacity animation
  48. MBProgressHUDAnimationFade,
  49. /// Opacity + scale animation (zoom in when appearing zoom out when disappearing)
  50. MBProgressHUDAnimationZoom,
  51. /// Opacity + scale animation (zoom out style)
  52. MBProgressHUDAnimationZoomOut,
  53. /// Opacity + scale animation (zoom in style)
  54. MBProgressHUDAnimationZoomIn
  55. };
  56. typedef NS_ENUM(NSInteger, MBProgressHUDBackgroundStyle) {
  57. /// Solid color background
  58. MBProgressHUDBackgroundStyleSolidColor,
  59. /// UIVisualEffectView or UIToolbar.layer background view
  60. MBProgressHUDBackgroundStyleBlur
  61. };
  62. typedef void (^MBProgressHUDCompletionBlock)(void);
  63. NS_ASSUME_NONNULL_BEGIN
  64. /**
  65. * Displays a simple HUD window containing a progress indicator and two optional labels for short messages.
  66. *
  67. * This is a simple drop-in class for displaying a progress HUD view similar to Apple's private UIProgressHUD class.
  68. * The MBProgressHUD window spans over the entire space given to it by the initWithFrame: constructor and catches all
  69. * user input on this region, thereby preventing the user operations on components below the view.
  70. *
  71. * @note To still allow touches to pass through the HUD, you can set hud.userInteractionEnabled = NO.
  72. * @attention MBProgressHUD is a UI class and should therefore only be accessed on the main thread.
  73. */
  74. @interface MBProgressHUD : UIView
  75. /**
  76. * Creates a new HUD, adds it to provided view and shows it. The counterpart to this method is hideHUDForView:animated:.
  77. *
  78. * @note This method sets removeFromSuperViewOnHide. The HUD will automatically be removed from the view hierarchy when hidden.
  79. *
  80. * @param view The view that the HUD will be added to
  81. * @param animated If set to YES the HUD will appear using the current animationType. If set to NO the HUD will not use
  82. * animations while appearing.
  83. * @return A reference to the created HUD.
  84. *
  85. * @see hideHUDForView:animated:
  86. * @see animationType
  87. */
  88. + (instancetype)showHUDAddedTo:(UIView *)view animated:(BOOL)animated;
  89. /// @name Showing and hiding
  90. /**
  91. * Finds the top-most HUD subview that hasn't finished and hides it. The counterpart to this method is showHUDAddedTo:animated:.
  92. *
  93. * @note This method sets removeFromSuperViewOnHide. The HUD will automatically be removed from the view hierarchy when hidden.
  94. *
  95. * @param view The view that is going to be searched for a HUD subview.
  96. * @param animated If set to YES the HUD will disappear using the current animationType. If set to NO the HUD will not use
  97. * animations while disappearing.
  98. * @return YES if a HUD was found and removed, NO otherwise.
  99. *
  100. * @see showHUDAddedTo:animated:
  101. * @see animationType
  102. */
  103. + (BOOL)hideHUDForView:(UIView *)view animated:(BOOL)animated;
  104. /**
  105. * Finds the top-most HUD subview that hasn't finished and returns it.
  106. *
  107. * @param view The view that is going to be searched.
  108. * @return A reference to the last HUD subview discovered.
  109. */
  110. + (nullable MBProgressHUD *)HUDForView:(UIView *)view NS_SWIFT_NAME(forView(_:));
  111. /**
  112. * A convenience constructor that initializes the HUD with the view's bounds. Calls the designated constructor with
  113. * view.bounds as the parameter.
  114. *
  115. * @param view The view instance that will provide the bounds for the HUD. Should be the same instance as
  116. * the HUD's superview (i.e., the view that the HUD will be added to).
  117. */
  118. - (instancetype)initWithView:(UIView *)view;
  119. /**
  120. * Displays the HUD.
  121. *
  122. * @note You need to make sure that the main thread completes its run loop soon after this method call so that
  123. * the user interface can be updated. Call this method when your task is already set up to be executed in a new thread
  124. * (e.g., when using something like NSOperation or making an asynchronous call like NSURLRequest).
  125. *
  126. * @param animated If set to YES the HUD will appear using the current animationType. If set to NO the HUD will not use
  127. * animations while appearing.
  128. *
  129. * @see animationType
  130. */
  131. - (void)showAnimated:(BOOL)animated;
  132. /**
  133. * Hides the HUD. This still calls the hudWasHidden: delegate. This is the counterpart of the show: method. Use it to
  134. * hide the HUD when your task completes.
  135. *
  136. * @param animated If set to YES the HUD will disappear using the current animationType. If set to NO the HUD will not use
  137. * animations while disappearing.
  138. *
  139. * @see animationType
  140. */
  141. - (void)hideAnimated:(BOOL)animated;
  142. /**
  143. * Hides the HUD after a delay. This still calls the hudWasHidden: delegate. This is the counterpart of the show: method. Use it to
  144. * hide the HUD when your task completes.
  145. *
  146. * @param animated If set to YES the HUD will disappear using the current animationType. If set to NO the HUD will not use
  147. * animations while disappearing.
  148. * @param delay Delay in seconds until the HUD is hidden.
  149. *
  150. * @see animationType
  151. */
  152. - (void)hideAnimated:(BOOL)animated afterDelay:(NSTimeInterval)delay;
  153. /**
  154. * The HUD delegate object. Receives HUD state notifications.
  155. */
  156. @property (weak, nonatomic) id<MBProgressHUDDelegate> delegate;
  157. /**
  158. * Called after the HUD is hidden.
  159. */
  160. @property (copy, nullable) MBProgressHUDCompletionBlock completionBlock;
  161. /**
  162. * Grace period is the time (in seconds) that the invoked method may be run without
  163. * showing the HUD. If the task finishes before the grace time runs out, the HUD will
  164. * not be shown at all.
  165. * This may be used to prevent HUD display for very short tasks.
  166. * Defaults to 0 (no grace time).
  167. * @note The graceTime needs to be set before the hud is shown. You thus can't use `showHUDAddedTo:animated:`,
  168. * but instead need to alloc / init the HUD, configure the grace time and than show it manually.
  169. */
  170. @property (assign, nonatomic) NSTimeInterval graceTime;
  171. /**
  172. * The minimum time (in seconds) that the HUD is shown.
  173. * This avoids the problem of the HUD being shown and than instantly hidden.
  174. * Defaults to 0 (no minimum show time).
  175. */
  176. @property (assign, nonatomic) NSTimeInterval minShowTime;
  177. /**
  178. * Removes the HUD from its parent view when hidden.
  179. * Defaults to NO.
  180. */
  181. @property (assign, nonatomic) BOOL removeFromSuperViewOnHide;
  182. /// @name Appearance
  183. /**
  184. * MBProgressHUD operation mode. The default is MBProgressHUDModeIndeterminate.
  185. */
  186. @property (assign, nonatomic) MBProgressHUDMode mode;
  187. /**
  188. * A color that gets forwarded to all labels and supported indicators. Also sets the tintColor
  189. * for custom views on iOS 7+. Set to nil to manage color individually.
  190. * Defaults to semi-translucent black on iOS 7 and later and white on earlier iOS versions.
  191. */
  192. @property (strong, nonatomic, nullable) UIColor *contentColor UI_APPEARANCE_SELECTOR;
  193. /**
  194. * The animation type that should be used when the HUD is shown and hidden.
  195. */
  196. @property (assign, nonatomic) MBProgressHUDAnimation animationType UI_APPEARANCE_SELECTOR;
  197. /**
  198. * The bezel offset relative to the center of the view. You can use MBProgressMaxOffset
  199. * and -MBProgressMaxOffset to move the HUD all the way to the screen edge in each direction.
  200. * E.g., CGPointMake(0.f, MBProgressMaxOffset) would position the HUD centered on the bottom edge.
  201. */
  202. @property (assign, nonatomic) CGPoint offset UI_APPEARANCE_SELECTOR;
  203. /**
  204. * The amount of space between the HUD edge and the HUD elements (labels, indicators or custom views).
  205. * This also represents the minimum bezel distance to the edge of the HUD view.
  206. * Defaults to 20.f
  207. */
  208. @property (assign, nonatomic) CGFloat margin UI_APPEARANCE_SELECTOR;
  209. /**
  210. * The minimum size of the HUD bezel. Defaults to CGSizeZero (no minimum size).
  211. */
  212. @property (assign, nonatomic) CGSize minSize UI_APPEARANCE_SELECTOR;
  213. /**
  214. * Force the HUD dimensions to be equal if possible.
  215. */
  216. @property (assign, nonatomic, getter = isSquare) BOOL square UI_APPEARANCE_SELECTOR;
  217. /**
  218. * When enabled, the bezel center gets slightly affected by the device accelerometer data.
  219. * Defaults to NO.
  220. *
  221. * @note This can cause main thread checker assertions on certain devices. https://github.com/jdg/MBProgressHUD/issues/552
  222. */
  223. @property (assign, nonatomic, getter=areDefaultMotionEffectsEnabled) BOOL defaultMotionEffectsEnabled UI_APPEARANCE_SELECTOR;
  224. /// @name Progress
  225. /**
  226. * The progress of the progress indicator, from 0.0 to 1.0. Defaults to 0.0.
  227. */
  228. @property (assign, nonatomic) float progress;
  229. /// @name ProgressObject
  230. /**
  231. * The NSProgress object feeding the progress information to the progress indicator.
  232. */
  233. @property (strong, nonatomic, nullable) NSProgress *progressObject;
  234. /// @name Views
  235. /**
  236. * The view containing the labels and indicator (or customView).
  237. */
  238. @property (strong, nonatomic, readonly) MBBackgroundView *bezelView;
  239. /**
  240. * View covering the entire HUD area, placed behind bezelView.
  241. */
  242. @property (strong, nonatomic, readonly) MBBackgroundView *backgroundView;
  243. /**
  244. * The UIView (e.g., a UIImageView) to be shown when the HUD is in MBProgressHUDModeCustomView.
  245. * The view should implement intrinsicContentSize for proper sizing. For best results use approximately 37 by 37 pixels.
  246. */
  247. @property (strong, nonatomic, nullable) UIView *customView;
  248. /**
  249. * A label that holds an optional short message to be displayed below the activity indicator. The HUD is automatically resized to fit
  250. * the entire text.
  251. */
  252. @property (strong, nonatomic, readonly) UILabel *label;
  253. /**
  254. * A label that holds an optional details message displayed below the labelText message. The details text can span multiple lines.
  255. */
  256. @property (strong, nonatomic, readonly) UILabel *detailsLabel;
  257. /**
  258. * A button that is placed below the labels. Visible only if a target / action is added and a title is assigned..
  259. */
  260. @property (strong, nonatomic, readonly) UIButton *button;
  261. @end
  262. @protocol MBProgressHUDDelegate <NSObject>
  263. @optional
  264. /**
  265. * Called after the HUD was fully hidden from the screen.
  266. */
  267. - (void)hudWasHidden:(MBProgressHUD *)hud;
  268. @end
  269. /**
  270. * A progress view for showing definite progress by filling up a circle (pie chart).
  271. */
  272. @interface MBRoundProgressView : UIView
  273. /**
  274. * Progress (0.0 to 1.0)
  275. */
  276. @property (nonatomic, assign) float progress;
  277. /**
  278. * Indicator progress color.
  279. * Defaults to white [UIColor whiteColor].
  280. */
  281. @property (nonatomic, strong) UIColor *progressTintColor;
  282. /**
  283. * Indicator background (non-progress) color.
  284. * Only applicable on iOS versions older than iOS 7.
  285. * Defaults to translucent white (alpha 0.1).
  286. */
  287. @property (nonatomic, strong) UIColor *backgroundTintColor;
  288. /*
  289. * Display mode - NO = round or YES = annular. Defaults to round.
  290. */
  291. @property (nonatomic, assign, getter = isAnnular) BOOL annular;
  292. @end
  293. /**
  294. * A flat bar progress view.
  295. */
  296. @interface MBBarProgressView : UIView
  297. /**
  298. * Progress (0.0 to 1.0)
  299. */
  300. @property (nonatomic, assign) float progress;
  301. /**
  302. * Bar border line color.
  303. * Defaults to white [UIColor whiteColor].
  304. */
  305. @property (nonatomic, strong) UIColor *lineColor;
  306. /**
  307. * Bar background color.
  308. * Defaults to clear [UIColor clearColor];
  309. */
  310. @property (nonatomic, strong) UIColor *progressRemainingColor;
  311. /**
  312. * Bar progress color.
  313. * Defaults to white [UIColor whiteColor].
  314. */
  315. @property (nonatomic, strong) UIColor *progressColor;
  316. @end
  317. @interface MBBackgroundView : UIView
  318. /**
  319. * The background style.
  320. * Defaults to MBProgressHUDBackgroundStyleBlur.
  321. */
  322. @property (nonatomic) MBProgressHUDBackgroundStyle style;
  323. /**
  324. * The blur effect style, when using MBProgressHUDBackgroundStyleBlur.
  325. * Defaults to UIBlurEffectStyleLight.
  326. */
  327. @property (nonatomic) UIBlurEffectStyle blurEffectStyle;
  328. /**
  329. * The background color or the blur tint color.
  330. *
  331. * Defaults to nil on iOS 13 and later and
  332. * `[UIColor colorWithWhite:0.8f alpha:0.6f]`
  333. * on older systems.
  334. */
  335. @property (nonatomic, strong, nullable) UIColor *color;
  336. @end
  337. NS_ASSUME_NONNULL_END