All Products
Search
Document Center

Toast component

Last Updated: May 25, 2021

AUToast defines Toast controls for mPaaS. AUToast is developed from APToast of APCommonUI. Use AUToast instead of APToast.

This component contains two types of Toast controls:

  • Common Toast
  • Modal Toast

The modal Toast has a transparent background layer, but the common Toast does not. Users cannot click the area covered by the background layer.

Sample images

API description

 
  1. // The declaration of the log output function, which is set by the external system.
  2. typedef void(*AUToastLogFunc)(NSString *tag, NSString *format, ...);
  3. extern AUToastLogFunc g_ToastExternLogFunc; // The global variables of the log output function are set by external system.
  4. #define AUToastLog(fmt, ...) {if(g_ToastExternLogFunc)g_ToastExternLogFunc(@"@AUToast",fmt,##__VA_ARGS__);}
  5. #define AUToast_Default_Duration 2.0 // AUToast Default display duration.
  6. #define AUToast_Strong_Duration 1.5 // AUToast Display duration of the strong prompt.
  7. #define AUToast_Weak_Duration 1.0 // AUToast Display duration of the weak prompt.
  8. /**
  9. * Add a new toast icon to the end of the existing icons instead of in the middle. Otherwise, an error will occurs in business use.
  10. */
  11. typedef enum{
  12. AUToastIconNone = 0, // No icon.
  13. AUToastIconSuccess, // The success icon.
  14. AUToastIconFailure, // The failure icon.
  15. AUToastIconLoading, // The loading icon.
  16. AUToastIconNetFailure, // Network failure.
  17. AUToastIconSecurityScan,// Security scanning.
  18. AUToastIconNetError, // The network error causing connection failure.
  19. AUToastIconProgress, // The loading icon indicating the loading progress.
  20. AUToastIconAlert, // The alarm icon.
  21. } AUToastIcon;
  22. /**
  23. * The Toast control.
  24. */
  25. @interface AUToast : UIView
  26. @property (nonatomic, assign) CGFloat xOffset; // Set the offset to the central point of the parent view in the x-axis.
  27. @property (nonatomic, assign) CGFloat yOffset; // Set the offset to the central point of the parent view in the y-axis.
  28. /*
  29. * The modal display prompt displayed in the key window. The system does not respond to user operations.
  30. * Call the dismissToast method to hide the Toast.
  31. *
  32. * @param text The displayed text. Default value: loading.
  33. * @param logTag The log ID.
  34. *
  35. * @return The displayed Toast object.
  36. */
  37. + (AUToast *)presentToastWithText:(NSString *)text
  38. logTag:(NSString*)logTag;
  39. /**
  40. * Show the Toast. To hide the Toast, call the dismissToast method.
  41. *
  42. * @param superview The parent view.
  43. * @param text Displayed text.
  44. * @param logTag The log tag.
  45. *
  46. * @return The displayed Toast object.
  47. */
  48. + (AUToast *)presentToastWithin:(UIView *)superview
  49. text:(NSString *)text
  50. logTag:(NSString*)logTag;
  51. /**
  52. * Show the Toast. To hide the Toast, call the dismissToast method.
  53. *
  54. * @param superview The parent view.
  55. * @param icon The icon type.
  56. * @param text Displayed text.
  57. * @param logTag The log tag.
  58. *
  59. * @return The displayed Toast object.
  60. */
  61. + (AUToast *)presentToastWithin:(UIView *)superview
  62. withIcon:(AUToastIcon)icon
  63. text:(NSString *)text
  64. logTag:(NSString*)logTag;
  65. /**
  66. * Show the Toast.
  67. *
  68. * @param superview The parent view.
  69. * @param icon The icon type.
  70. * @param text Displayed text.
  71. * @param duration The display duration.
  72. * @param logTag The log tag.
  73. *
  74. * @return The displayed Toast object.
  75. */
  76. + (AUToast *)presentToastWithin:(UIView *)superview
  77. withIcon:(AUToastIcon)icon
  78. text:(NSString *)text
  79. duration:(NSTimeInterval)duration
  80. logTag:(NSString*)logTag;
  81. /**
  82. * Show the Toast.
  83. *
  84. * @param superview In which view of Toast is displayed.
  85. * @param icon The icon type.
  86. * @param text Displayed text.
  87. * @param duration Display duration.
  88. * @param logTag The log tag.
  89. * @param completion Callback after Toast automatically disappears.
  90. *
  91. * @return The displayed Toast object.
  92. */
  93. + (AUToast *)presentToastWithin:(UIView *)superview
  94. withIcon:(AUToastIcon)icon
  95. text:(NSString *)text
  96. duration:(NSTimeInterval)duration
  97. logTag:(NSString*)logTag
  98. completion:(void (^)())completion;
  99. /**
  100. * Show the Toast.
  101. *
  102. * @param superview In which the view of Toast is displayed.
  103. * @param icon The icon type.
  104. * @param text Displayed text.
  105. * @param duration Display duration.
  106. * @param delay Display delay duration.
  107. * @param logTag The log tag.
  108. * @param completion Callback after Toast automatically disappears.
  109. *
  110. * @return The displayed Toast object.
  111. */
  112. + (AUToast *)presentToastWithin:(UIView *)superview
  113. withIcon:(AUToastIcon)icon
  114. text:(NSString *)text
  115. duration:(NSTimeInterval)duration
  116. delay:(NSTimeInterval)delay
  117. logTag:(NSString*)logTag
  118. completion:(void (^)())completion;
  119. /*
  120. * Show the modal Toast. To hide the Toast, call the dismissToast method.
  121. * Different from the common Toast, the modal Toast has a transparent background layer, which prevents users from clicking the screen.
  122. *
  123. * @param superview The parent view.
  124. * @param text Displayed text.
  125. * @param logTag The log tag.
  126. *
  127. * @return The displayed Toast object.
  128. */
  129. + (AUToast *)presentModelToastWithin:(UIView *)superview
  130. text:(NSString *)text
  131. logTag:(NSString*)logTag;
  132. /**
  133. * Show the modal Toast.
  134. * Different from the common Toast, the modal Toast has a transparent background layer, which prevents users from clicking the screen.
  135. *
  136. * @param superview In which view of Toast is displayed.
  137. * @param icon The icon type.
  138. * @param text Displayed text.
  139. * @param duration Display duration.
  140. * @param logTag The log tag.
  141. * @param completion Callback after Toast automatically disappears.
  142. *
  143. * @return The displayed Toast object.
  144. */
  145. + (AUToast *)presentModalToastWithin:(UIView *)superview
  146. withIcon:(AUToastIcon)icon
  147. text:(NSString *)text
  148. duration:(NSTimeInterval)duration
  149. logTag:(NSString*)logTag
  150. completion:(void (^)())completion;
  151. /**
  152. * Show the modal Toast.
  153. * Different from the common Toast, the modal Toast has a transparent background layer, which prevents users from clicking the screen.
  154. *
  155. * @param superview In which the view of Toast is displayed.
  156. * @param icon Icon type.
  157. * @param text Displayed text.
  158. * @param duration Display duration.
  159. * @param delay Display delay duration.
  160. * @param logTag The log tag.
  161. * @param completion Callback after Toast automatically disappears.
  162. *
  163. * @return The displayed Toast object.
  164. */
  165. + (AUToast *)presentModalToastWithin:(UIView *)superview
  166. withIcon:(AUToastIcon)icon
  167. text:(NSString *)text
  168. duration:(NSTimeInterval)duration
  169. delay:(NSTimeInterval)delay
  170. logTag:(NSString*)logTag
  171. completion:(void (^)())completion;
  172. /*
  173. * Hide the Toast.
  174. */
  175. - (void)dismissToast;
  176. /**
  177. * Set the prefix text of the progress. If this parameter is not set, the default value is "Loading data".
  178. * The setting is effective only when the Toast type is AUToastIconProgress. Otherwise, ignore this parameter.
  179. *
  180. * @param prefix The text.
  181. */
  182. - (void)setProgressPrefix:(NSString*)prefix;
  183. /**
  184. * Show the data loading progress in percentage.
  185. * The setting is effective only when the Toast type is AUToastIconProgress. Otherwise, ignore this parameter.
  186. *
  187. * @param value Currently loaded data. The value range is 0.0 to 1.0.
  188. *
  189. */
  190. - (void)setProgressText:(float)value;
  191. @end

Sample code

 
  1. [AUToast presentToastWithin:self.view withIcon:AUToastIconNetFailure text:@"系统繁忙" logTag:@"demo"];
  2. [AUToast presentToastWithin:self.view withIcon:AUToastIconSuccess text:@"成功提示" logTag:@"demo"];
  3. [AUToast presentToastWithin:self.view withIcon:AUToastIconFailure text:@"失败提示" logTag:@"demo"];
  4. [AUToast presentToastWithin:self.view withIcon:AUToastIconAlert text:@"警示提示" logTag:@"demo"];
  5. // Loading.
  6. [AUToast presentToastWithin:self.view withIcon:AUToastIconLoading text:nil logTag:@"demo"];
  7. // Set the scenario where the progress appears.
  8. AUToast *toast = [AUToast presentToastWithin:self.view withIcon:AUToastIconProgress text:@"加载中" logTag:@"demo"];
  9. toast.origin = point;
  10. [toast setProgressPrefix:@"~~~"];
  11. [toast setProgressText:0.5];
  12. // The modal Toast.
  13. [AUToast presentModalToastWithin:weakSelf.view withIcon:AUToastIconLoading text:@"模态toast,最长文案有这么多,算不算多啊(三秒后消失)" duration:3 logTag:@"demo" completion:NULL];
  14. [AUToast presentModalToastWithin:weakSelf.view withIcon:AUToastIconLoading text:@"模态toast,最长文案有这么多,算不算多啊(三秒后消失)" duration:3 delay:2 logTag:@"demo" completion:NULL];