iOS开发之类似安卓的Toast短暂提示框

来源:互联网 发布:俄罗斯 共青城 知乎 编辑:程序博客网 时间:2024/06/05 17:34

向凡神致敬~


在Android中具有确认提示框与短暂提示框Toast,但在iOS中只有确认提示框Alert并无类似于Android的短暂提示框Toast。

注:使用此组件控制器需加入到UINavigationController中

使用方法:

[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. [[[ZFToast alloc] init] popUpToastWithMessage:@"提示内容"];  

源码:

ZFToast.h文件

[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  *  @file 
  3.  *  @author 张凡 
  4.  *  @date 2016/3/25 
  5.  */  
  6. #import <UIKit/UIKit.h>  
  7.   
  8. /** 
  9.  *  @class ZFToast 
  10.  *  @brief Toast弹窗控件 
  11.  *  @author 张凡 
  12.  *  @date 2016/3/25 
  13.  */  
  14.   
  15. /** 
  16.  *  @使用方法 
  17.  *  [[[ZFToast alloc] init] popUpToastWithMessage:@"提示内容"]; 
  18.  */  
  19.   
  20. @interface ZFToast : UIView  
  21.   
  22. /// @brief 文本提示框  
  23. - (void)popUpToastWithMessage:(NSString *)message;  
  24.   
  25. @end</span><span style="color:#cc0000;">  
  26. </span>  

ZFToast.m文件

[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. import "ZFToast.h"  
  2.   
  3. @interface ZFToast ()  
  4.   
  5. /// @brief 存放文本的UILabel  
  6. @property (strong,nonatomicUILabel *textLabel;  
  7. @property (strong,nonatomicZFToast *toast;  
  8. @property (strong,nonatomicNSTimer *timer;  
  9. /// @brief 记录是否移除  
  10. @property (assign,nonatomic) NSInteger currentDate;  
  11.   
  12. @end  
  13. @implementation ZFToast  
  14.   
  15. - (instancetype)init  
  16. {  
  17.     self = [super init];  
  18.     if (self) {  
  19.         self.currentDate = 0;  
  20.     }  
  21.       
  22.     return self;  
  23. }  
  24.   
  25. - (void)popUpToastWithMessage:(NSString *)message  
  26. {  
  27.     /// @brief 创建定时器  
  28.     [self createTimer];  
  29.     /// @brief 初始化Label  
  30.     [self initLabel:message];  
  31.     /// @brief 初始化底层视图  
  32.     [self initBottomView];  
  33.       
  34. }  
  35.   
  36. #pragma mark - 创建定时器  
  37. - (void)createTimer  
  38. {  
  39.     self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];  
  40.       
  41.     //暂停定时器  
  42.     [self.timer setFireDate:[NSDate distantFuture]];  
  43.       
  44. }  
  45.   
  46. #pragma mark - 初始化Label  
  47. - (void)initLabel:(NSString *)message  
  48. {  
  49.     //获取屏幕宽度  
  50.     CGFloat screenWidth = CGRectGetWidth([[UIScreen mainScreen] bounds]);  
  51.     //获取屏幕高度  
  52.     CGFloat screenHeight = CGRectGetHeight([[UIScreen mainScreen] bounds]);  
  53.     /// @brief Label的字号  
  54.     UIFont *font = [UIFont systemFontOfSize:15];  
  55.     /// @brief 控件的宽  
  56.     CGFloat width = screenWidth / 3.0 * 2.0;  
  57.     /// @brief Label所需的宽高  
  58.     CGSize labelSize = [self calculationTextNeedSize:message andFont:15 andWidth:width];  
  59.     self.textLabel = [[UILabel alloc] initWithFrame:CGRectMake(1010, labelSize.width, labelSize.height)];  
  60.     self.textLabel.backgroundColor = [UIColor clearColor];  
  61.     self.textLabel.textColor = [UIColor colorWithRed:29.0/255.0 green:29.0/255.0 blue:29.0/255.0 alpha:1];  
  62.     self.textLabel.font = font;  
  63.     self.textLabel.text = message;  
  64.     self.textLabel.numberOfLines = 0;  
  65.     self.textLabel.textAlignment = NSTextAlignmentCenter;  
  66. }  
  67.   
  68. #pragma mark - 初始化底层视图  
  69. - (void)initBottomView  
  70. {  
  71.     //获取屏幕宽度  
  72.     CGFloat screenWidth = CGRectGetWidth([[UIScreen mainScreen] bounds]);  
  73.     //获取屏幕高度  
  74.     CGFloat screenHeight = CGRectGetHeight([[UIScreen mainScreen] bounds]);  
  75.     self.backgroundColor = [UIColor colorWithRed:204.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:0.8];  
  76.     self.frame = CGRectMake((screenWidth - self.textLabel.frame.size.width)/2.0, screenHeight/3.0 * 2.0 + screenHeight/3.0/2.0self.textLabel.frame.size.width + 20self.textLabel.frame.size.height + 20);  
  77.     [self addSubview:self.textLabel];  
  78.     //设置ImageView是否可以设为圆角  
  79.     self.layer.masksToBounds = YES;  
  80.     //设置圆角度数  
  81.     self.layer.cornerRadius = 10;  
  82.     UIViewController *vc = [self getCurrentVC];  
  83.     [vc.view addSubview:self];  
  84.     //启动定时器  
  85.     [self.timer setFireDate:[NSDate distantPast]];  
  86. }  
  87.   
  88. #pragma mark - 取到当前控制器  
  89. - (UIViewController *)getCurrentVC  
  90. {  
  91.     UIViewController *result = nil;  
  92.       
  93.     UIWindow * window = [[UIApplication sharedApplication] keyWindow];  
  94.     if (window.windowLevel != UIWindowLevelNormal)  
  95.     {  
  96.         NSArray *windows = [[UIApplication sharedApplication] windows];  
  97.         for(UIWindow * tmpWin in windows)  
  98.         {  
  99.             if (tmpWin.windowLevel == UIWindowLevelNormal)  
  100.             {  
  101.                 window = tmpWin;  
  102.                 break;  
  103.             }  
  104.         }  
  105.     }  
  106.       
  107.     UIView *frontView = [[window subviews] objectAtIndex:0];  
  108.     id nextResponder = [frontView nextResponder];  
  109.       
  110.     if ([nextResponder isKindOfClass:[UIViewController class]])  
  111.         result = nextResponder;  
  112.     else  
  113.         result = window.rootViewController;  
  114.       
  115.     return result;  
  116. }  
  117.   
  118. - (void)onTimer  
  119. {  
  120.     self.currentDate++;  
  121.     if (self.currentDate == 2) {  
  122.         //暂停定时器  
  123.         [self.timer setFireDate:[NSDate distantFuture]];  
  124.         [self removeFromSuperview];  
  125.         self.currentDate = 0;  
  126.     }  
  127.       
  128. }  
  129.   
  130. #pragma mark - 计算文本所需大小  
  131. - (CGSize)calculationTextNeedSize:(NSString *)text andFont:(CGFloat)font andWidth:(CGFloat)width  
  132. {  
  133.     CGSize labelSize = [text sizeWithFont: [UIFont boldSystemFontOfSize:font]  
  134.                         constrainedToSize: CGSizeMake(width, MAXFLOAT )  
  135.                             lineBreakMode: UILineBreakModeWordWrap];  
  136.       
  137.     return labelSize;  
  138. }  
  139.   
  140. @end  

本文有因为问题请联系

QQ:563699115

Telephone:18341266547

1 0