ZDTostView.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // ZDTostView.m
  3. // Zhidao
  4. //
  5. // Created by zhuchao on 13-10-21.
  6. // Copyright (c) 2013年 Baidu. All rights reserved.
  7. //
  8. #import "ZDTostView.h"
  9. @interface ZDTostView()
  10. - (ZDTostView *)showInView:(UIView *)aView;
  11. @end
  12. @implementation ZDTostView
  13. - (id)initWithFrame:(CGRect)frame
  14. {
  15. self = [super initWithFrame:frame];
  16. if (self) {
  17. self.customImageOffSet = CGPointMake(0.0f, 9.0f);
  18. self.label2.hidden = YES;
  19. [self.label1 setFont:[UIFont systemFontOfSize:13.0f]];
  20. }
  21. return self;
  22. }
  23. - (void)setMessage:(NSString *)aMessage
  24. {
  25. [self.label1 setText:aMessage];
  26. [self.label2 setHidden:YES];
  27. }
  28. - (void)setCustomImage:(UIImage *)image
  29. {
  30. [self.customView removeFromSuperview];
  31. self.customView = nil;
  32. [self.customImageView setHidden:NO];
  33. [self.customImageView setImage:image];
  34. }
  35. + (ZDTostView *)sharedTostView {
  36. static ZDTostView *_sharedTostView = nil;
  37. static dispatch_once_t onceToken;
  38. dispatch_once(&onceToken, ^{
  39. _sharedTostView = [[ZDTostView alloc] initWithFrame:[UIScreen mainScreen].bounds];
  40. });
  41. return _sharedTostView;
  42. }
  43. + (ZDTostView *)errorMessageView:(NSString *)aMessage
  44. {
  45. ZDTostView *tost = [ZDTostView sharedTostView];
  46. NSBundle *bundle = [NSBundle bundleForClass:[self class]];
  47. UIImage *image = [UIImage imageNamed:@"审批错误" inBundle:bundle compatibleWithTraitCollection:nil];
  48. [tost setCustomImage:image];
  49. [tost.label1 setText:aMessage];
  50. return tost;
  51. }
  52. + (ZDTostView *)showErrorMessage:(NSString *)aMessage inView:(UIView *)aView
  53. {
  54. return [[self errorMessageView:aMessage] showSharedTostInViewAutoDisappear:aView];
  55. }
  56. + (ZDTostView *)succeedMessageView:(NSString *)aMessage
  57. {
  58. ZDTostView *tost = [ZDTostView sharedTostView];
  59. NSBundle *bundle = [NSBundle bundleForClass:[self class]];
  60. UIImage *image = [UIImage imageNamed:@"审批对号" inBundle:bundle compatibleWithTraitCollection:nil];
  61. [tost setCustomImage:image];
  62. [tost.label1 setText:aMessage];
  63. return tost;
  64. }
  65. + (ZDTostView *)showSucceedMessage:(NSString *)aMessage inView:(UIView *)aView finish:(void (^)(void))finish
  66. {
  67. return [[self succeedMessageView:aMessage] showSharedTostInViewAutoDisappear:aView finish:finish];
  68. }
  69. + (ZDTostView *)showSucceedMessage:(NSString *)aMessage inView:(UIView *)aView
  70. {
  71. return [self showSucceedMessage:aMessage inView:aView finish:nil];
  72. }
  73. + (ZDTostView *)netErrorView:(NSString *)aMessage
  74. {
  75. ZDTostView *tost = [ZDTostView sharedTostView];
  76. NSBundle *bundle = [NSBundle bundleForClass:[self class]];
  77. UIImage *image = [UIImage imageNamed:@"审批错误" inBundle:bundle compatibleWithTraitCollection:nil];
  78. [tost setCustomImage:image];
  79. [tost.label1 setText:aMessage];
  80. return tost;
  81. }
  82. - (ZDTostView *)showInView:(UIView *)aView
  83. {
  84. [super showInView:aView];
  85. return [ZDTostView sharedTostView];
  86. }
  87. + (ZDTostView *)showNetErrorWithMessage:(NSString *)aMessage inView:(UIView *)aView
  88. {
  89. return [[self netErrorView:aMessage] showSharedTostInViewAutoDisappear:aView];
  90. }
  91. - (ZDTostView *)showSharedTostInViewAutoDisappear:(UIView *)aView finish:(void (^)(void))finish
  92. {
  93. return [ZDTostView showSharedTostInViewAutoDisappear:aView finish:finish];
  94. }
  95. + (ZDTostView *)showSharedTostInViewAutoDisappear:(UIView *)aView finish:(void (^)(void))finish
  96. {
  97. if ([ZDTostView sharedTostView].superview!=nil) {
  98. [[ZDTostView sharedTostView] hideView];
  99. }
  100. [[ZDTostView sharedTostView] showInView:aView];
  101. double delayInSeconds = 0.7;
  102. dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
  103. dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
  104. [[ZDTostView sharedTostView] setCustomView:nil];
  105. [[ZDTostView sharedTostView] hideView];
  106. // if (!isNull(finish)) {
  107. // finish();
  108. // }
  109. });
  110. return [ZDTostView sharedTostView];
  111. }
  112. - (ZDTostView *)showSharedTostInViewAutoDisappear:(UIView *)aView
  113. {
  114. return [self showSharedTostInViewAutoDisappear:aView finish:nil];
  115. }
  116. + (ZDTostView *)showSharedTostInViewAutoDisappear:(UIView *)aView
  117. {
  118. return [self showSharedTostInViewAutoDisappear:aView finish:nil];
  119. }
  120. + (ZDTostView *)loadingView:(NSString *)aMessage
  121. {
  122. ZDTostView *tost = [ZDTostView sharedTostView];
  123. UIActivityIndicatorView *customView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  124. tost.customViewOffSet = CGPointMake(0.0f, 40.0f);
  125. [customView startAnimating];
  126. [tost setCustomView:customView];
  127. [tost.label1 setText:aMessage];
  128. return tost;
  129. }
  130. + (ZDTostView *)showLoadingMessage:(NSString *)aMessage inView:(UIView *)aView
  131. {
  132. return [[self loadingView:aMessage] showInView:aView];
  133. }
  134. + (void)removeLoadingTost
  135. {
  136. ZDTostView *tost = [ZDTostView sharedTostView];
  137. if (tost.customView && [tost.customView isKindOfClass:[UIActivityIndicatorView class]] && [tost superview]) {
  138. [tost hideView];
  139. }
  140. }
  141. + (ZDTostView *)showErrorMessage2:(NSString *)aMessage inView:(UIView *)aView{
  142. ZDTostView *tost = [ZDTostView sharedTostView];
  143. NSBundle *bundle = [NSBundle bundleForClass:[self class]];
  144. UIImage *image = [UIImage imageNamed:@"审批对号" inBundle:bundle compatibleWithTraitCollection:nil];
  145. [tost setCustomImage:image];
  146. [tost.label1 setText:aMessage];
  147. return [tost showSharedTostInViewAutoDisappear:aView];
  148. }
  149. + (ZDTostView *)showWaitMessage:(NSString *)aMessage inView:(UIView *)aView{
  150. ZDTostView *tost = [ZDTostView sharedTostView];
  151. UIImage *image = IMG(@"提示");
  152. [tost setCustomImage:image];
  153. [tost.label1 setText:aMessage];
  154. return [tost showSharedTostInViewAutoDisappear:aView];
  155. }
  156. @end