Toast.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // Toast.m
  3. // AJPushDemo
  4. //
  5. // Created by kean_qi on 2021/3/18.
  6. //
  7. #import "Toast.h"
  8. #import <QuartzCore/CALayer.h>
  9. //设备的高
  10. #define size_height1 [UIScreen mainScreen].bounds.size.height
  11. //设备的宽
  12. #define size_width1 [UIScreen mainScreen].bounds.size.width
  13. static Toast * _toast = nil;
  14. static NSInteger shake = 0;
  15. static NSTimer *timer = nil;
  16. @interface Toast ()
  17. @property (retain, nonatomic) UILabel *label;
  18. @end
  19. @implementation Toast
  20. - (id)initWithText:(NSString*)text
  21. {
  22. self = [super init];
  23. if (self) {
  24. _text = [text copy];
  25. // Initialization code
  26. UIFont *font = [UIFont systemFontOfSize:16];
  27. CGRect textRect = [_text boundingRectWithSize:(CGSizeMake(280, MAXFLOAT)) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName: font} context:nil];
  28. CGSize textSize = textRect.size;
  29. //leak;
  30. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, textSize.width, textSize.height)];
  31. label.backgroundColor = [UIColor clearColor];
  32. label.textColor = [UIColor whiteColor];
  33. label.font = font;
  34. label.text = _text;
  35. label.textAlignment = NSTextAlignmentCenter;
  36. label.numberOfLines = 0;
  37. label.shadowColor = [UIColor darkGrayColor];
  38. label.shadowOffset = CGSizeMake(1, 1);
  39. self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
  40. CGRect rect;
  41. rect.size = CGSizeMake(textSize.width + 20, textSize.height + 10);
  42. rect.origin = CGPointMake((size_width1-rect.size.width)/2, 380);
  43. self.layer.cornerRadius = 5;
  44. self.layer.masksToBounds = YES;
  45. [self setFrame:rect];
  46. [self addSubview:label];
  47. }
  48. return self;
  49. }
  50. - (id)initWithUpText:(NSString*)text
  51. {
  52. self = [super init];
  53. if (self) {
  54. _text = [text copy];
  55. // Initialization code
  56. UIFont *font = [UIFont systemFontOfSize:16];
  57. CGRect textRect = [_text boundingRectWithSize:(CGSizeMake(280, MAXFLOAT)) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName: font} context:nil];
  58. CGSize textSize = textRect.size;
  59. //leak;
  60. _label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, textSize.width, textSize.height)];
  61. _label.backgroundColor = [UIColor clearColor];
  62. _label.textColor = [UIColor whiteColor];
  63. _label.font = font;
  64. _label.text = _text;
  65. _label.numberOfLines = 0;
  66. _label.textAlignment = NSTextAlignmentCenter;
  67. _label.shadowColor = [UIColor darkGrayColor];
  68. _label.shadowOffset = CGSizeMake(1, 1);
  69. self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
  70. CGRect rect;
  71. rect.size = CGSizeMake(textSize.width + 20, textSize.height + 10);
  72. rect.origin = CGPointMake((size_width1-rect.size.width)/2, 104);
  73. self.layer.cornerRadius = 5;
  74. self.layer.masksToBounds = YES;
  75. [self setFrame:rect];
  76. [self addSubview:_label];
  77. }
  78. return self;
  79. }
  80. +(Toast *)makeText:(NSString *)text{
  81. @synchronized(self){
  82. if(_toast == nil){
  83. _toast = [[Toast alloc]initWithText:text];
  84. }
  85. }
  86. return _toast;
  87. }
  88. +(Toast *)makeUpText:(NSString *)text{
  89. @synchronized(self){
  90. if(_toast == nil){
  91. _toast = [[Toast alloc] initWithUpText:text];
  92. }
  93. }
  94. return _toast;
  95. }
  96. -(void)showWithType:(enum AJTimeType)type{
  97. if (type == AJLongTime) {
  98. _time = 3.0f;
  99. }else{
  100. _time = 1.0f;
  101. }
  102. UIWindow *window = [[UIApplication sharedApplication] keyWindow];
  103. timer = [NSTimer timerWithTimeInterval:(_time/4.0f) target:self selector:@selector(removeToast) userInfo:nil repeats:NO];
  104. [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
  105. [window addSubview:self];
  106. }
  107. -(void)removeToast
  108. {
  109. [UIView animateWithDuration:_time animations:^{
  110. if (_toast.alpha!=0.0f)
  111. _toast.alpha -= 0.3f;
  112. } completion:^(BOOL finished) {
  113. [_toast setAlpha:0];
  114. [_toast removeFromSuperview];
  115. _toast = nil;
  116. }];
  117. }
  118. + (void)classRemoveToast{
  119. [UIView animateWithDuration:2.0f animations:^{
  120. if (_toast.alpha!=0.0f)
  121. _toast.alpha -= 0.3f;
  122. } completion:^(BOOL finished) {
  123. [_toast setAlpha:0];
  124. [_toast removeFromSuperview];
  125. _toast = nil;
  126. }];
  127. }
  128. +(void)aj_makeUpText:(NSString *)text showWithType:(enum AJTimeType)type;{
  129. // init
  130. @synchronized (self) {
  131. if(_toast == nil){
  132. shake=0;
  133. _toast = [[Toast alloc] initWithUpText:text];
  134. }else shake=1;
  135. }
  136. // show
  137. CGFloat time;
  138. if (type == AJLongTime) {
  139. time = 3.0f;
  140. }else{
  141. time = 1.0f;
  142. }
  143. if (shake) {
  144. dispatch_async(dispatch_get_main_queue(), ^{
  145. _toast.label.text = text;
  146. });
  147. //创建动画
  148. CAKeyframeAnimation * keyAnimaion = [CAKeyframeAnimation animation];
  149. keyAnimaion.keyPath = @"transform.rotation";
  150. keyAnimaion.values = @[@(-10 / 180.0 * M_PI),@(10 /180.0 * M_PI),@(-10/ 180.0 * M_PI),@(0/ 180.0 * M_PI)];//度数转弧度
  151. keyAnimaion.removedOnCompletion = YES;
  152. keyAnimaion.fillMode = kCAFillModeForwards;
  153. keyAnimaion.duration = 0.15;
  154. keyAnimaion.repeatCount = 1;
  155. [_toast.layer addAnimation:keyAnimaion forKey:nil];
  156. }else{
  157. UIWindow *window = [[UIApplication sharedApplication] keyWindow];
  158. timer = [NSTimer timerWithTimeInterval:(time/4.0f) target:self selector:@selector(classRemoveToast) userInfo:nil repeats:NO];
  159. [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
  160. [window addSubview:_toast];
  161. }
  162. }
  163. @end