MBProgressHUD.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. //
  2. // MBProgressHUD.h
  3. // Version 0.9.2
  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 (c) 2009-2015 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. @protocol MBProgressHUDDelegate;
  30. typedef NS_ENUM(NSInteger, MBProgressHUDMode) {
  31. /** Progress is shown using an UIActivityIndicatorView. This is the default. */
  32. MBProgressHUDModeIndeterminate,
  33. /** Progress is shown using a round, pie-chart like, progress view. */
  34. MBProgressHUDModeDeterminate,
  35. /** Progress is shown using a horizontal progress bar */
  36. MBProgressHUDModeDeterminateHorizontalBar,
  37. /** Progress is shown using a ring-shaped progress view. */
  38. MBProgressHUDModeAnnularDeterminate,
  39. /** Shows a custom view */
  40. MBProgressHUDModeCustomView,
  41. /** Shows only labels */
  42. MBProgressHUDModeText
  43. };
  44. typedef NS_ENUM(NSInteger, MBProgressHUDAnimation) {
  45. /** Opacity animation */
  46. MBProgressHUDAnimationFade,
  47. /** Opacity + scale animation */
  48. MBProgressHUDAnimationZoom,
  49. MBProgressHUDAnimationZoomOut = MBProgressHUDAnimationZoom,
  50. MBProgressHUDAnimationZoomIn
  51. };
  52. #ifndef MB_INSTANCETYPE
  53. #if __has_feature(objc_instancetype)
  54. #define MB_INSTANCETYPE instancetype
  55. #else
  56. #define MB_INSTANCETYPE id
  57. #endif
  58. #endif
  59. #ifndef MB_STRONG
  60. #if __has_feature(objc_arc)
  61. #define MB_STRONG strong
  62. #else
  63. #define MB_STRONG retain
  64. #endif
  65. #endif
  66. #ifndef MB_WEAK
  67. #if __has_feature(objc_arc_weak)
  68. #define MB_WEAK weak
  69. #elif __has_feature(objc_arc)
  70. #define MB_WEAK unsafe_unretained
  71. #else
  72. #define MB_WEAK assign
  73. #endif
  74. #endif
  75. #if NS_BLOCKS_AVAILABLE
  76. typedef void (^MBProgressHUDCompletionBlock)();
  77. #endif
  78. /**
  79. * Displays a simple HUD window containing a progress indicator and two optional labels for short messages.
  80. *
  81. * This is a simple drop-in class for displaying a progress HUD view similar to Apple's private UIProgressHUD class.
  82. * The MBProgressHUD window spans over the entire space given to it by the initWithFrame constructor and catches all
  83. * user input on this region, thereby preventing the user operations on components below the view. The HUD itself is
  84. * drawn centered as a rounded semi-transparent view which resizes depending on the user specified content.
  85. *
  86. * This view supports four modes of operation:
  87. * - MBProgressHUDModeIndeterminate - shows a UIActivityIndicatorView
  88. * - MBProgressHUDModeDeterminate - shows a custom round progress indicator
  89. * - MBProgressHUDModeAnnularDeterminate - shows a custom annular progress indicator
  90. * - MBProgressHUDModeCustomView - shows an arbitrary, user specified view (see `customView`)
  91. *
  92. * All three modes can have optional labels assigned:
  93. * - If the labelText property is set and non-empty then a label containing the provided content is placed below the
  94. * indicator view.
  95. * - If also the detailsLabelText property is set then another label is placed below the first label.
  96. */
  97. @interface MBProgressHUD : UIView
  98. /**
  99. * Creates a new HUD, adds it to provided view and shows it. The counterpart to this method is hideHUDForView:animated:.
  100. *
  101. * @note This method sets `removeFromSuperViewOnHide`. The HUD will automatically be removed from the view hierarchy when hidden.
  102. *
  103. * @param view The view that the HUD will be added to
  104. * @param animated If set to YES the HUD will appear using the current animationType. If set to NO the HUD will not use
  105. * animations while appearing.
  106. * @return A reference to the created HUD.
  107. *
  108. * @see hideHUDForView:animated:
  109. * @see animationType
  110. */
  111. + (MB_INSTANCETYPE)showHUDAddedTo:(UIView *)view animated:(BOOL)animated;
  112. /**
  113. * Finds the top-most HUD subview and hides it. The counterpart to this method is showHUDAddedTo:animated:.
  114. *
  115. * @note This method sets `removeFromSuperViewOnHide`. The HUD will automatically be removed from the view hierarchy when hidden.
  116. *
  117. * @param view The view that is going to be searched for a HUD subview.
  118. * @param animated If set to YES the HUD will disappear using the current animationType. If set to NO the HUD will not use
  119. * animations while disappearing.
  120. * @return YES if a HUD was found and removed, NO otherwise.
  121. *
  122. * @see showHUDAddedTo:animated:
  123. * @see animationType
  124. */
  125. + (BOOL)hideHUDForView:(UIView *)view animated:(BOOL)animated;
  126. /**
  127. * Finds all the HUD subviews and hides them.
  128. *
  129. * @note This method sets `removeFromSuperViewOnHide`. The HUDs will automatically be removed from the view hierarchy when hidden.
  130. *
  131. * @param view The view that is going to be searched for HUD subviews.
  132. * @param animated If set to YES the HUDs will disappear using the current animationType. If set to NO the HUDs will not use
  133. * animations while disappearing.
  134. * @return the number of HUDs found and removed.
  135. *
  136. * @see hideHUDForView:animated:
  137. * @see animationType
  138. */
  139. + (NSUInteger)hideAllHUDsForView:(UIView *)view animated:(BOOL)animated;
  140. /**
  141. * Finds the top-most HUD subview and returns it.
  142. *
  143. * @param view The view that is going to be searched.
  144. * @return A reference to the last HUD subview discovered.
  145. */
  146. + (MB_INSTANCETYPE)HUDForView:(UIView *)view;
  147. /**
  148. * Finds all HUD subviews and returns them.
  149. *
  150. * @param view The view that is going to be searched.
  151. * @return All found HUD views (array of MBProgressHUD objects).
  152. */
  153. + (NSArray *)allHUDsForView:(UIView *)view;
  154. /**
  155. * A convenience constructor that initializes the HUD with the window's bounds. Calls the designated constructor with
  156. * window.bounds as the parameter.
  157. *
  158. * @param window The window instance that will provide the bounds for the HUD. Should be the same instance as
  159. * the HUD's superview (i.e., the window that the HUD will be added to).
  160. */
  161. - (id)initWithWindow:(UIWindow *)window;
  162. /**
  163. * A convenience constructor that initializes the HUD with the view's bounds. Calls the designated constructor with
  164. * view.bounds as the parameter
  165. *
  166. * @param view The view instance that will provide the bounds for the HUD. Should be the same instance as
  167. * the HUD's superview (i.e., the view that the HUD will be added to).
  168. */
  169. - (id)initWithView:(UIView *)view;
  170. /**
  171. * Display the HUD. You need to make sure that the main thread completes its run loop soon after this method call so
  172. * the user interface can be updated. Call this method when your task is already set-up to be executed in a new thread
  173. * (e.g., when using something like NSOperation or calling an asynchronous call like NSURLRequest).
  174. *
  175. * @param animated If set to YES the HUD will appear using the current animationType. If set to NO the HUD will not use
  176. * animations while appearing.
  177. *
  178. * @see animationType
  179. */
  180. - (void)show:(BOOL)animated;
  181. /**
  182. * Hide the HUD. This still calls the hudWasHidden: delegate. This is the counterpart of the show: method. Use it to
  183. * hide the HUD when your task completes.
  184. *
  185. * @param animated If set to YES the HUD will disappear using the current animationType. If set to NO the HUD will not use
  186. * animations while disappearing.
  187. *
  188. * @see animationType
  189. */
  190. - (void)hide:(BOOL)animated;
  191. /**
  192. * Hide the HUD after a delay. This still calls the hudWasHidden: delegate. This is the counterpart of the show: method. Use it to
  193. * hide the HUD when your task completes.
  194. *
  195. * @param animated If set to YES the HUD will disappear using the current animationType. If set to NO the HUD will not use
  196. * animations while disappearing.
  197. * @param delay Delay in seconds until the HUD is hidden.
  198. *
  199. * @see animationType
  200. */
  201. - (void)hide:(BOOL)animated afterDelay:(NSTimeInterval)delay;
  202. /**
  203. * Shows the HUD while a background task is executing in a new thread, then hides the HUD.
  204. *
  205. * This method also takes care of autorelease pools so your method does not have to be concerned with setting up a
  206. * pool.
  207. *
  208. * @param method The method to be executed while the HUD is shown. This method will be executed in a new thread.
  209. * @param target The object that the target method belongs to.
  210. * @param object An optional object to be passed to the method.
  211. * @param animated If set to YES the HUD will (dis)appear using the current animationType. If set to NO the HUD will not use
  212. * animations while (dis)appearing.
  213. */
  214. - (void)showWhileExecuting:(SEL)method onTarget:(id)target withObject:(id)object animated:(BOOL)animated;
  215. #if NS_BLOCKS_AVAILABLE
  216. /**
  217. * Shows the HUD while a block is executing on a background queue, then hides the HUD.
  218. *
  219. * @see showAnimated:whileExecutingBlock:onQueue:completionBlock:
  220. */
  221. - (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block;
  222. /**
  223. * Shows the HUD while a block is executing on a background queue, then hides the HUD.
  224. *
  225. * @see showAnimated:whileExecutingBlock:onQueue:completionBlock:
  226. */
  227. - (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block completionBlock:(MBProgressHUDCompletionBlock)completion;
  228. /**
  229. * Shows the HUD while a block is executing on the specified dispatch queue, then hides the HUD.
  230. *
  231. * @see showAnimated:whileExecutingBlock:onQueue:completionBlock:
  232. */
  233. - (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block onQueue:(dispatch_queue_t)queue;
  234. /**
  235. * Shows the HUD while a block is executing on the specified dispatch queue, executes completion block on the main queue, and then hides the HUD.
  236. *
  237. * @param animated If set to YES the HUD will (dis)appear using the current animationType. If set to NO the HUD will
  238. * not use animations while (dis)appearing.
  239. * @param block The block to be executed while the HUD is shown.
  240. * @param queue The dispatch queue on which the block should be executed.
  241. * @param completion The block to be executed on completion.
  242. *
  243. * @see completionBlock
  244. */
  245. - (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block onQueue:(dispatch_queue_t)queue
  246. completionBlock:(MBProgressHUDCompletionBlock)completion;
  247. /**
  248. * A block that gets called after the HUD was completely hidden.
  249. */
  250. @property (copy) MBProgressHUDCompletionBlock completionBlock;
  251. #endif
  252. /**
  253. * MBProgressHUD operation mode. The default is MBProgressHUDModeIndeterminate.
  254. *
  255. * @see MBProgressHUDMode
  256. */
  257. @property (assign) MBProgressHUDMode mode;
  258. /**
  259. * The animation type that should be used when the HUD is shown and hidden.
  260. *
  261. * @see MBProgressHUDAnimation
  262. */
  263. @property (assign) MBProgressHUDAnimation animationType;
  264. /**
  265. * The UIView (e.g., a UIImageView) to be shown when the HUD is in MBProgressHUDModeCustomView.
  266. * For best results use a 37 by 37 pixel view (so the bounds match the built in indicator bounds).
  267. */
  268. @property (MB_STRONG) UIView *customView;
  269. /**
  270. * The HUD delegate object.
  271. *
  272. * @see MBProgressHUDDelegate
  273. */
  274. @property (MB_WEAK) id<MBProgressHUDDelegate> delegate;
  275. /**
  276. * An optional short message to be displayed below the activity indicator. The HUD is automatically resized to fit
  277. * the entire text. If the text is too long it will get clipped by displaying "..." at the end. If left unchanged or
  278. * set to @"", then no message is displayed.
  279. */
  280. @property (copy) NSString *labelText;
  281. /**
  282. * An optional details message displayed below the labelText message. This message is displayed only if the labelText
  283. * property is also set and is different from an empty string (@""). The details text can span multiple lines.
  284. */
  285. @property (copy) NSString *detailsLabelText;
  286. /**
  287. * The opacity of the HUD window. Defaults to 0.8 (80% opacity).
  288. */
  289. @property (assign) float opacity;
  290. /**
  291. * The color of the HUD window. Defaults to black. If this property is set, color is set using
  292. * this UIColor and the opacity property is not used. using retain because performing copy on
  293. * UIColor base colors (like [UIColor greenColor]) cause problems with the copyZone.
  294. */
  295. @property (MB_STRONG) UIColor *color;
  296. /**
  297. * The x-axis offset of the HUD relative to the centre of the superview.
  298. */
  299. @property (assign) float xOffset;
  300. /**
  301. * The y-axis offset of the HUD relative to the centre of the superview.
  302. */
  303. @property (assign) float yOffset;
  304. /**
  305. * The amount of space between the HUD edge and the HUD elements (labels, indicators or custom views).
  306. * Defaults to 20.0
  307. */
  308. @property (assign) float margin;
  309. /**
  310. * The corner radius for the HUD
  311. * Defaults to 10.0
  312. */
  313. @property (assign) float cornerRadius;
  314. /**
  315. * Cover the HUD background view with a radial gradient.
  316. */
  317. @property (assign) BOOL dimBackground;
  318. /*
  319. * Grace period is the time (in seconds) that the invoked method may be run without
  320. * showing the HUD. If the task finishes before the grace time runs out, the HUD will
  321. * not be shown at all.
  322. * This may be used to prevent HUD display for very short tasks.
  323. * Defaults to 0 (no grace time).
  324. * Grace time functionality is only supported when the task status is known!
  325. * @see taskInProgress
  326. */
  327. @property (assign) float graceTime;
  328. /**
  329. * The minimum time (in seconds) that the HUD is shown.
  330. * This avoids the problem of the HUD being shown and than instantly hidden.
  331. * Defaults to 0 (no minimum show time).
  332. */
  333. @property (assign) float minShowTime;
  334. /**
  335. * Indicates that the executed operation is in progress. Needed for correct graceTime operation.
  336. * If you don't set a graceTime (different than 0.0) this does nothing.
  337. * This property is automatically set when using showWhileExecuting:onTarget:withObject:animated:.
  338. * When threading is done outside of the HUD (i.e., when the show: and hide: methods are used directly),
  339. * you need to set this property when your task starts and completes in order to have normal graceTime
  340. * functionality.
  341. */
  342. @property (assign) BOOL taskInProgress;
  343. /**
  344. * Removes the HUD from its parent view when hidden.
  345. * Defaults to NO.
  346. */
  347. @property (assign) BOOL removeFromSuperViewOnHide;
  348. /**
  349. * Font to be used for the main label. Set this property if the default is not adequate.
  350. */
  351. @property (MB_STRONG) UIFont* labelFont;
  352. /**
  353. * Color to be used for the main label. Set this property if the default is not adequate.
  354. */
  355. @property (MB_STRONG) UIColor* labelColor;
  356. /**
  357. * Font to be used for the details label. Set this property if the default is not adequate.
  358. */
  359. @property (MB_STRONG) UIFont* detailsLabelFont;
  360. /**
  361. * Color to be used for the details label. Set this property if the default is not adequate.
  362. */
  363. @property (MB_STRONG) UIColor* detailsLabelColor;
  364. /**
  365. * The color of the activity indicator. Defaults to [UIColor whiteColor]
  366. * Does nothing on pre iOS 5.
  367. */
  368. @property (MB_STRONG) UIColor *activityIndicatorColor;
  369. /**
  370. * The progress of the progress indicator, from 0.0 to 1.0. Defaults to 0.0.
  371. */
  372. @property (assign) float progress;
  373. /**
  374. * The minimum size of the HUD bezel. Defaults to CGSizeZero (no minimum size).
  375. */
  376. @property (assign) CGSize minSize;
  377. /**
  378. * The actual size of the HUD bezel.
  379. * You can use this to limit touch handling on the bezel area only.
  380. * @see https://github.com/jdg/MBProgressHUD/pull/200
  381. */
  382. @property (atomic, assign, readonly) CGSize size;
  383. /**
  384. * Force the HUD dimensions to be equal if possible.
  385. */
  386. @property (assign, getter = isSquare) BOOL square;
  387. @end
  388. @protocol MBProgressHUDDelegate <NSObject>
  389. @optional
  390. /**
  391. * Called after the HUD was fully hidden from the screen.
  392. */
  393. - (void)hudWasHidden:(MBProgressHUD *)hud;
  394. @end
  395. /**
  396. * A progress view for showing definite progress by filling up a circle (pie chart).
  397. */
  398. @interface MBRoundProgressView : UIView
  399. /**
  400. * Progress (0.0 to 1.0)
  401. */
  402. @property (nonatomic, assign) float progress;
  403. /**
  404. * Indicator progress color.
  405. * Defaults to white [UIColor whiteColor]
  406. */
  407. @property (nonatomic, MB_STRONG) UIColor *progressTintColor;
  408. /**
  409. * Indicator background (non-progress) color.
  410. * Defaults to translucent white (alpha 0.1)
  411. */
  412. @property (nonatomic, MB_STRONG) UIColor *backgroundTintColor;
  413. /*
  414. * Display mode - NO = round or YES = annular. Defaults to round.
  415. */
  416. @property (nonatomic, assign, getter = isAnnular) BOOL annular;
  417. @end
  418. /**
  419. * A flat bar progress view.
  420. */
  421. @interface MBBarProgressView : UIView
  422. /**
  423. * Progress (0.0 to 1.0)
  424. */
  425. @property (nonatomic, assign) float progress;
  426. /**
  427. * Bar border line color.
  428. * Defaults to white [UIColor whiteColor].
  429. */
  430. @property (nonatomic, MB_STRONG) UIColor *lineColor;
  431. /**
  432. * Bar background color.
  433. * Defaults to clear [UIColor clearColor];
  434. */
  435. @property (nonatomic, MB_STRONG) UIColor *progressRemainingColor;
  436. /**
  437. * Bar progress color.
  438. * Defaults to white [UIColor whiteColor].
  439. */
  440. @property (nonatomic, MB_STRONG) UIColor *progressColor;
  441. @end