No Description

MBProgressHUD.h 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. //
  2. // MBProgressHUD.h
  3. // Version 0.8
  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) 2013 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 enum {
  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. } MBProgressHUDMode;
  44. typedef enum {
  45. /** Opacity animation */
  46. MBProgressHUDAnimationFade,
  47. /** Opacity + scale animation */
  48. MBProgressHUDAnimationZoom,
  49. MBProgressHUDAnimationZoomOut = MBProgressHUDAnimationZoom,
  50. MBProgressHUDAnimationZoomIn
  51. } MBProgressHUDAnimation;
  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. * @param view The view that the HUD will be added to
  102. * @param animated If set to YES the HUD will appear using the current animationType. If set to NO the HUD will not use
  103. * animations while appearing.
  104. * @return A reference to the created HUD.
  105. *
  106. * @see hideHUDForView:animated:
  107. * @see animationType
  108. */
  109. + (MB_INSTANCETYPE)showHUDAddedTo:(UIView *)view animated:(BOOL)animated;
  110. /**
  111. * Finds the top-most HUD subview and hides it. The counterpart to this method is showHUDAddedTo:animated:.
  112. *
  113. * @param view The view that is going to be searched for a HUD subview.
  114. * @param animated If set to YES the HUD will disappear using the current animationType. If set to NO the HUD will not use
  115. * animations while disappearing.
  116. * @return YES if a HUD was found and removed, NO otherwise.
  117. *
  118. * @see showHUDAddedTo:animated:
  119. * @see animationType
  120. */
  121. + (BOOL)hideHUDForView:(UIView *)view animated:(BOOL)animated;
  122. /**
  123. * Finds all the HUD subviews and hides them.
  124. *
  125. * @param view The view that is going to be searched for HUD subviews.
  126. * @param animated If set to YES the HUDs will disappear using the current animationType. If set to NO the HUDs will not use
  127. * animations while disappearing.
  128. * @return the number of HUDs found and removed.
  129. *
  130. * @see hideHUDForView:animated:
  131. * @see animationType
  132. */
  133. + (NSUInteger)hideAllHUDsForView:(UIView *)view animated:(BOOL)animated;
  134. /**
  135. * Finds the top-most HUD subview and returns it.
  136. *
  137. * @param view The view that is going to be searched.
  138. * @return A reference to the last HUD subview discovered.
  139. */
  140. + (MB_INSTANCETYPE)HUDForView:(UIView *)view;
  141. /**
  142. * Finds all HUD subviews and returns them.
  143. *
  144. * @param view The view that is going to be searched.
  145. * @return All found HUD views (array of MBProgressHUD objects).
  146. */
  147. + (NSArray *)allHUDsForView:(UIView *)view;
  148. /**
  149. * A convenience constructor that initializes the HUD with the window's bounds. Calls the designated constructor with
  150. * window.bounds as the parameter.
  151. *
  152. * @param window The window instance that will provide the bounds for the HUD. Should be the same instance as
  153. * the HUD's superview (i.e., the window that the HUD will be added to).
  154. */
  155. - (id)initWithWindow:(UIWindow *)window;
  156. /**
  157. * A convenience constructor that initializes the HUD with the view's bounds. Calls the designated constructor with
  158. * view.bounds as the parameter
  159. *
  160. * @param view The view instance that will provide the bounds for the HUD. Should be the same instance as
  161. * the HUD's superview (i.e., the view that the HUD will be added to).
  162. */
  163. - (id)initWithView:(UIView *)view;
  164. /**
  165. * Display the HUD. You need to make sure that the main thread completes its run loop soon after this method call so
  166. * the user interface can be updated. Call this method when your task is already set-up to be executed in a new thread
  167. * (e.g., when using something like NSOperation or calling an asynchronous call like NSURLRequest).
  168. *
  169. * @param animated If set to YES the HUD will appear using the current animationType. If set to NO the HUD will not use
  170. * animations while appearing.
  171. *
  172. * @see animationType
  173. */
  174. - (void)show:(BOOL)animated;
  175. /**
  176. * Hide the HUD. This still calls the hudWasHidden: delegate. This is the counterpart of the show: method. Use it to
  177. * hide the HUD when your task completes.
  178. *
  179. * @param animated If set to YES the HUD will disappear using the current animationType. If set to NO the HUD will not use
  180. * animations while disappearing.
  181. *
  182. * @see animationType
  183. */
  184. - (void)hide:(BOOL)animated;
  185. /**
  186. * Hide the HUD after a delay. This still calls the hudWasHidden: delegate. This is the counterpart of the show: method. Use it to
  187. * hide the HUD when your task completes.
  188. *
  189. * @param animated If set to YES the HUD will disappear using the current animationType. If set to NO the HUD will not use
  190. * animations while disappearing.
  191. * @param delay Delay in seconds until the HUD is hidden.
  192. *
  193. * @see animationType
  194. */
  195. - (void)hide:(BOOL)animated afterDelay:(NSTimeInterval)delay;
  196. /**
  197. * Shows the HUD while a background task is executing in a new thread, then hides the HUD.
  198. *
  199. * This method also takes care of autorelease pools so your method does not have to be concerned with setting up a
  200. * pool.
  201. *
  202. * @param method The method to be executed while the HUD is shown. This method will be executed in a new thread.
  203. * @param target The object that the target method belongs to.
  204. * @param object An optional object to be passed to the method.
  205. * @param animated If set to YES the HUD will (dis)appear using the current animationType. If set to NO the HUD will not use
  206. * animations while (dis)appearing.
  207. */
  208. - (void)showWhileExecuting:(SEL)method onTarget:(id)target withObject:(id)object animated:(BOOL)animated;
  209. #if NS_BLOCKS_AVAILABLE
  210. /**
  211. * Shows the HUD while a block is executing on a background queue, then hides the HUD.
  212. *
  213. * @see showAnimated:whileExecutingBlock:onQueue:completionBlock:
  214. */
  215. - (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block;
  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 completionBlock:(MBProgressHUDCompletionBlock)completion;
  222. /**
  223. * Shows the HUD while a block is executing on the specified dispatch queue, then hides the HUD.
  224. *
  225. * @see showAnimated:whileExecutingBlock:onQueue:completionBlock:
  226. */
  227. - (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block onQueue:(dispatch_queue_t)queue;
  228. /**
  229. * 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.
  230. *
  231. * @param animated If set to YES the HUD will (dis)appear using the current animationType. If set to NO the HUD will
  232. * not use animations while (dis)appearing.
  233. * @param block The block to be executed while the HUD is shown.
  234. * @param queue The dispatch queue on which the block should be executed.
  235. * @param completion The block to be executed on completion.
  236. *
  237. * @see completionBlock
  238. */
  239. - (void)showAnimated:(BOOL)animated whileExecutingBlock:(dispatch_block_t)block onQueue:(dispatch_queue_t)queue
  240. completionBlock:(MBProgressHUDCompletionBlock)completion;
  241. /**
  242. * A block that gets called after the HUD was completely hidden.
  243. */
  244. @property (copy) MBProgressHUDCompletionBlock completionBlock;
  245. #endif
  246. /**
  247. * MBProgressHUD operation mode. The default is MBProgressHUDModeIndeterminate.
  248. *
  249. * @see MBProgressHUDMode
  250. */
  251. @property (assign) MBProgressHUDMode mode;
  252. /**
  253. * The animation type that should be used when the HUD is shown and hidden.
  254. *
  255. * @see MBProgressHUDAnimation
  256. */
  257. @property (assign) MBProgressHUDAnimation animationType;
  258. /**
  259. * The UIView (e.g., a UIImageView) to be shown when the HUD is in MBProgressHUDModeCustomView.
  260. * For best results use a 37 by 37 pixel view (so the bounds match the built in indicator bounds).
  261. */
  262. @property (MB_STRONG) UIView *customView;
  263. /**
  264. * The HUD delegate object.
  265. *
  266. * @see MBProgressHUDDelegate
  267. */
  268. @property (MB_WEAK) id<MBProgressHUDDelegate> delegate;
  269. /**
  270. * An optional short message to be displayed below the activity indicator. The HUD is automatically resized to fit
  271. * the entire text. If the text is too long it will get clipped by displaying "..." at the end. If left unchanged or
  272. * set to @"", then no message is displayed.
  273. */
  274. @property (copy) NSString *labelText;
  275. /**
  276. * An optional details message displayed below the labelText message. This message is displayed only if the labelText
  277. * property is also set and is different from an empty string (@""). The details text can span multiple lines.
  278. */
  279. @property (copy) NSString *detailsLabelText;
  280. /**
  281. * The opacity of the HUD window. Defaults to 0.8 (80% opacity).
  282. */
  283. @property (assign) float opacity;
  284. /**
  285. * The color of the HUD window. Defaults to black. If this property is set, color is set using
  286. * this UIColor and the opacity property is not used. using retain because performing copy on
  287. * UIColor base colors (like [UIColor greenColor]) cause problems with the copyZone.
  288. */
  289. @property (MB_STRONG) UIColor *color;
  290. /**
  291. * The x-axis offset of the HUD relative to the centre of the superview.
  292. */
  293. @property (assign) float xOffset;
  294. /**
  295. * The y-axis offset of the HUD relative to the centre of the superview.
  296. */
  297. @property (assign) float yOffset;
  298. /**
  299. * The amount of space between the HUD edge and the HUD elements (labels, indicators or custom views).
  300. * Defaults to 20.0
  301. */
  302. @property (assign) float margin;
  303. /**
  304. * Cover the HUD background view with a radial gradient.
  305. */
  306. @property (assign) BOOL dimBackground;
  307. /*
  308. * Grace period is the time (in seconds) that the invoked method may be run without
  309. * showing the HUD. If the task finishes before the grace time runs out, the HUD will
  310. * not be shown at all.
  311. * This may be used to prevent HUD display for very short tasks.
  312. * Defaults to 0 (no grace time).
  313. * Grace time functionality is only supported when the task status is known!
  314. * @see taskInProgress
  315. */
  316. @property (assign) float graceTime;
  317. /**
  318. * The minimum time (in seconds) that the HUD is shown.
  319. * This avoids the problem of the HUD being shown and than instantly hidden.
  320. * Defaults to 0 (no minimum show time).
  321. */
  322. @property (assign) float minShowTime;
  323. /**
  324. * Indicates that the executed operation is in progress. Needed for correct graceTime operation.
  325. * If you don't set a graceTime (different than 0.0) this does nothing.
  326. * This property is automatically set when using showWhileExecuting:onTarget:withObject:animated:.
  327. * When threading is done outside of the HUD (i.e., when the show: and hide: methods are used directly),
  328. * you need to set this property when your task starts and completes in order to have normal graceTime
  329. * functionality.
  330. */
  331. @property (assign) BOOL taskInProgress;
  332. /**
  333. * Removes the HUD from its parent view when hidden.
  334. * Defaults to NO.
  335. */
  336. @property (assign) BOOL removeFromSuperViewOnHide;
  337. /**
  338. * Font to be used for the main label. Set this property if the default is not adequate.
  339. */
  340. @property (MB_STRONG) UIFont* labelFont;
  341. /**
  342. * Font to be used for the details label. Set this property if the default is not adequate.
  343. */
  344. @property (MB_STRONG) UIFont* detailsLabelFont;
  345. /**
  346. * The progress of the progress indicator, from 0.0 to 1.0. Defaults to 0.0.
  347. */
  348. @property (assign) float progress;
  349. /**
  350. * The minimum size of the HUD bezel. Defaults to CGSizeZero (no minimum size).
  351. */
  352. @property (assign) CGSize minSize;
  353. /**
  354. * Force the HUD dimensions to be equal if possible.
  355. */
  356. @property (assign, getter = isSquare) BOOL square;
  357. @end
  358. @protocol MBProgressHUDDelegate <NSObject>
  359. @optional
  360. /**
  361. * Called after the HUD was fully hidden from the screen.
  362. */
  363. - (void)hudWasHidden:(MBProgressHUD *)hud;
  364. @end
  365. /**
  366. * A progress view for showing definite progress by filling up a circle (pie chart).
  367. */
  368. @interface MBRoundProgressView : UIView
  369. /**
  370. * Progress (0.0 to 1.0)
  371. */
  372. @property (nonatomic, assign) float progress;
  373. /**
  374. * Indicator progress color.
  375. * Defaults to white [UIColor whiteColor]
  376. */
  377. @property (nonatomic, MB_STRONG) UIColor *progressTintColor;
  378. /**
  379. * Indicator background (non-progress) color.
  380. * Defaults to translucent white (alpha 0.1)
  381. */
  382. @property (nonatomic, MB_STRONG) UIColor *backgroundTintColor;
  383. /*
  384. * Display mode - NO = round or YES = annular. Defaults to round.
  385. */
  386. @property (nonatomic, assign, getter = isAnnular) BOOL annular;
  387. @end
  388. /**
  389. * A flat bar progress view.
  390. */
  391. @interface MBBarProgressView : UIView
  392. /**
  393. * Progress (0.0 to 1.0)
  394. */
  395. @property (nonatomic, assign) float progress;
  396. /**
  397. * Bar border line color.
  398. * Defaults to white [UIColor whiteColor].
  399. */
  400. @property (nonatomic, MB_STRONG) UIColor *lineColor;
  401. /**
  402. * Bar background color.
  403. * Defaults to clear [UIColor clearColor];
  404. */
  405. @property (nonatomic, MB_STRONG) UIColor *progressRemainingColor;
  406. /**
  407. * Bar progress color.
  408. * Defaults to white [UIColor whiteColor].
  409. */
  410. @property (nonatomic, MB_STRONG) UIColor *progressColor;
  411. @end