TDSProgressHUD.h 12 KB

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