ZFToast.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #import "ZFToast.h"
  2. #import "NSString+Category.h"
  3. NSTimeInterval const kShowLabelTime = 1.5;
  4. NSInteger const kRemoveTime = 2.0;
  5. @interface ZFToast ()
  6. /// @brief 存放文本的UILabel
  7. @property (strong,nonatomic) UILabel *textLabel;
  8. @property (strong,nonatomic) NSTimer *timer;
  9. /// @brief 记录是否移除
  10. @property (assign,nonatomic) NSInteger currentDate;
  11. @end
  12. @implementation ZFToast
  13. +(void)ShowWithMessage:(NSString *)message{
  14. ZFToast *toast = [[ZFToast alloc]init];
  15. [toast popUpToastWithMessage:message];
  16. }
  17. //- (instancetype)initWithMessage:(NSString *)message
  18. //{
  19. // self = [super init];
  20. // if (self) {
  21. //// self.currentDate = 0;
  22. // [self popUpToastWithMessage:message];
  23. // }
  24. //
  25. // return self;
  26. //}
  27. //有点..
  28. //+ (instancetype)shareClient
  29. //{
  30. // static ZFToast *zfToast = nil;
  31. //
  32. // static dispatch_once_t onceToken;
  33. // dispatch_once(&onceToken, ^{
  34. // zfToast = [[ZFToast alloc] init];
  35. //
  36. // });
  37. //
  38. // return zfToast;
  39. //}
  40. - (void)popUpToastWithMessage:(NSString *)message
  41. {
  42. // [self.textLabel removeFromSuperview];
  43. /// @brief 创建定时器
  44. // [self createTimer];
  45. if (![message isKindOfClass:NSString.class] || !message.length) {
  46. return;
  47. }
  48. dispatch_async(dispatch_get_main_queue(), ^{
  49. /// @brief 初始化Label
  50. [self initLabel:message];
  51. /// @brief 初始化底层视图
  52. [self initBottomView];
  53. });
  54. }
  55. #pragma mark - 创建定时器
  56. - (void)createTimer
  57. {
  58. self.timer = [NSTimer scheduledTimerWithTimeInterval:kShowLabelTime target:self selector:@selector(onTimer) userInfo:self repeats:YES];
  59. //暂停定时器
  60. [self.timer setFireDate:[NSDate distantFuture]];
  61. }
  62. #pragma mark - 初始化Label
  63. - (void)initLabel:(NSString *)message
  64. {
  65. //获取屏幕宽度
  66. CGFloat screenWidth = CGRectGetWidth([[UIScreen mainScreen] bounds]);
  67. //获取屏幕高度
  68. // CGFloat screenHeight = CGRectGetHeight([[UIScreen mainScreen] bounds]);
  69. /// @brief Label的字号
  70. UIFont *font = [UIFont systemFontOfSize:15];
  71. /// @brief 控件的宽
  72. CGFloat width = screenWidth / 3.0 * 2.0;
  73. /// @brief Label所需的宽高
  74. CGSize labelSize = [NSString calculationTextNeedSizeWithText:message font:15 width:width];
  75. // NSLog(@"%f",labelSize.width);
  76. // self.textLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, labelSize.width, labelSize.height)];
  77. // NSLog(@"%f",labelSize.width);
  78. // self.textLabel.backgroundColor = [UIColor clearColor];
  79. // self.textLabel.textColor = [UIColor whiteColor];
  80. self.textLabel.font = font;
  81. self.textLabel.text = message;
  82. self.textLabel.frame = CGRectMake(10, 10, labelSize.width, labelSize.height);
  83. // self.textLabel.numberOfLines = 0;
  84. // self.textLabel.textAlignment = NSTextAlignmentCenter;
  85. }
  86. #pragma mark - 初始化底层视图
  87. - (void)initBottomView
  88. {
  89. //获取屏幕宽度
  90. CGFloat screenWidth = CGRectGetWidth([[UIScreen mainScreen] bounds]);
  91. //获取屏幕高度
  92. CGFloat screenHeight = CGRectGetHeight([[UIScreen mainScreen] bounds]);
  93. self.backgroundColor = [UIColor colorWithRed:0.0/255.0 green:0.0/255.0 blue:0.0/255.0 alpha:0.8];
  94. self.frame = CGRectMake((screenWidth - self.textLabel.frame.size.width)/2.0, (screenHeight - self.textLabel.frame.size.height)/2.0, self.textLabel.frame.size.width + 20, self.textLabel.frame.size.height + 20);
  95. [self addSubview:self.textLabel];
  96. //设置ImageView是否可以设为圆角
  97. self.layer.masksToBounds = YES;
  98. //设置圆角度数
  99. self.layer.cornerRadius = 10;
  100. //位置可更改
  101. UIViewController *vc = [UIViewController getCurrentShowVC];
  102. // vc = (UIViewController *)[[UIApplication sharedApplication] keyWindow];
  103. self.center = vc.view.center;
  104. self.tag = 10001;
  105. [vc.view addSubview:self];
  106. //启动定时器
  107. // [self.timer setFireDate:[NSDate distantPast]];
  108. [UIView animateWithDuration:kShowLabelTime animations:^{
  109. self.alpha = 0.6;
  110. } completion:^(BOOL finished) {
  111. [self removeFromSuperview];
  112. }];
  113. }
  114. -(void)dealloc{
  115. // CheckRunWhere;
  116. }
  117. - (void)onTimer
  118. {
  119. self.currentDate++;
  120. if (self.currentDate == kRemoveTime) {
  121. //暂停定时器
  122. [self.timer setFireDate:[NSDate distantFuture]];
  123. [self.timer invalidate];
  124. self.timer = nil;
  125. [self removeFromSuperview];
  126. self.currentDate = 0;
  127. }
  128. }
  129. - (UILabel *)textLabel{
  130. if (!_textLabel) {
  131. self.textLabel = [[UILabel alloc] init];
  132. // NSLog(@"%f",labelSize.width);
  133. self.textLabel.backgroundColor = [UIColor clearColor];
  134. self.textLabel.textColor = [UIColor whiteColor];
  135. self.textLabel.numberOfLines = 0;
  136. self.textLabel.textAlignment = NSTextAlignmentCenter;
  137. }
  138. return _textLabel;
  139. }
  140. @end