UGCKitProgressHUD.h 17 KB

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