关于iOS中类似于Android的Toast短暂提示框
2016-03-26 00:10
489 查看
在Android中具有确认提示框与短暂提示框Toast,但在iOS中只有确认提示框Alert并无类似于Android的短暂提示框Toast,若需要可自己写一个,博主自己写了一个已上传至github,下载请点击
注:使用此组件控制器需加入到UINavigationController中
使用方法:
源码:
ZFToast.h文件
ZFToast.m文件
如有不明白的地方欢迎咨询:
QQ:294491256
Telephone:13390517636
注:使用此组件控制器需加入到UINavigationController中
使用方法:
[[[ZFToast alloc] init] popUpToastWithMessage:@"提示内容"];
源码:
ZFToast.h文件
/** * @file * @author 张凡 * @date 2016/3/25 */ #import <UIKit/UIKit.h> /** * @class ZFToast * @brief Toast弹窗控件 * @author 张凡 * @date 2016/3/25 */ /** * @使用方法 * [[[ZFToast alloc] init] popUpToastWithMessage:@"提示内容"]; */ @interface ZFToast : UIView /// @brief 文本提示框 - (void)popUpToastWithMessage:(NSString *)message; @end</span><span style="color:#cc0000;"> </span>
ZFToast.m文件
import "ZFToast.h" @interface ZFToast () /// @brief 存放文本的UILabel @property (strong,nonatomic) UILabel *textLabel; @property (strong,nonatomic) ZFToast *toast; @property (strong,nonatomic) NSTimer *timer; /// @brief 记录是否移除 @property (assign,nonatomic) NSInteger currentDate; @end @implementation ZFToast - (instancetype)init { self = [super init]; if (self) { self.currentDate = 0; } return self; } - (void)popUpToastWithMessage:(NSString *)message { /// @brief 创建定时器 [self createTimer]; /// @brief 初始化Label [self initLabel:message]; /// @brief 初始化底层视图 [self initBottomView]; } #pragma mark - 创建定时器 - (void)createTimer { self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(onTimer) userInfo:nil repeats:YES]; //暂停定时器 [self.timer setFireDate:[NSDate distantFuture]]; } #pragma mark - 初始化Label - (void)initLabel:(NSString *)message { //获取屏幕宽度 CGFloat screenWidth = CGRectGetWidth([[UIScreen mainScreen] bounds]); //获取屏幕高度 CGFloat screenHeight = CGRectGetHeight([[UIScreen mainScreen] bounds]); /// @brief Label的字号 UIFont *font = [UIFont systemFontOfSize:15]; /// @brief 控件的宽 CGFloat width = screenWidth / 3.0 * 2.0; /// @brief Label所需的宽高 CGSize labelSize = [self calculationTextNeedSize:message andFont:15 andWidth:width]; self.textLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, labelSize.width, labelSize.height)]; self.textLabel.backgroundColor = [UIColor clearColor]; self.textLabel.textColor = [UIColor colorWithRed:29.0/255.0 green:29.0/255.0 blue:29.0/255.0 alpha:1]; self.textLabel.font = font; self.textLabel.text = message; self.textLabel.numberOfLines = 0; self.textLabel.textAlignment = NSTextAlignmentCenter; } #pragma mark - 初始化底层视图 - (void)initBottomView { //获取屏幕宽度 CGFloat screenWidth = CGRectGetWidth([[UIScreen mainScreen] bounds]); //获取屏幕高度 CGFloat screenHeight = CGRectGetHeight([[UIScreen mainScreen] bounds]); self.backgroundColor = [UIColor colorWithRed:204.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:0.8]; self.frame = CGRectMake((screenWidth - self.textLabel.frame.size.width)/2.0, screenHeight/3.0 * 2.0 + screenHeight/3.0/2.0, self.textLabel.frame.size.width + 20, self.textLabel.frame.size.height + 20); [self addSubview:self.textLabel]; //设置ImageView是否可以设为圆角 self.layer.masksToBounds = YES; //设置圆角度数 self.layer.cornerRadius = 10; UIViewController *vc = [self getCurrentVC]; [vc.view addSubview:self]; //启动定时器 [self.timer setFireDate:[NSDate distantPast]]; } #pragma mark - 取到当前控制器 - (UIViewController *)getCurrentVC { UIViewController *result = nil; UIWindow * window = [[UIApplication sharedApplication] keyWindow]; if (window.windowLevel != UIWindowLevelNormal) { NSArray *windows = [[UIApplication sharedApplication] windows]; for(UIWindow * tmpWin in windows) { if (tmpWin.windowLevel == UIWindowLevelNormal) { window = tmpWin; break; } } } UIView *frontView = [[window subviews] objectAtIndex:0]; id nextResponder = [frontView nextResponder]; if ([nextResponder isKindOfClass:[UIViewController class]]) result = nextResponder; else result = window.rootViewController; return result; } - (void)onTimer { self.currentDate++; if (self.currentDate == 2) { //暂停定时器 [self.timer setFireDate:[NSDate distantFuture]]; [self removeFromSuperview]; self.currentDate = 0; } } #pragma mark - 计算文本所需大小 - (CGSize)calculationTextNeedSize:(NSString *)text andFont:(CGFloat)font andWidth:(CGFloat)width { CGSize labelSize = [text sizeWithFont: [UIFont boldSystemFontOfSize:font] constrainedToSize: CGSizeMake(width, MAXFLOAT ) lineBreakMode: UILineBreakModeWordWrap]; return labelSize; } @end
如有不明白的地方欢迎咨询:
QQ:294491256
Telephone:13390517636
相关文章推荐
- android RecyclerView首字母悬浮在顶部,滑动删除
- Android SDK Manager更新SDK终极办法(非翻墙)
- Android实现一键获取课程成绩dome
- Android起步之布局(1)LinearLayout
- Android的开源框架Android-Universal-Image-Loader的基本介绍
- android开发中遇到的安全问题
- 图片异步加载之 Android-Universal-Image-Loader
- Android中listview图片错乱问题及解决方案
- [Android]MAC JVM安装路径
- [android] activity横竖屏切换的生命周期
- android 自定义控件学习
- AndroidKiller反编译失败
- Android Studio SDK 代理地址设置
- Android涉及到的设计模式(转)
- Android数据库存储
- [android] activity的启动模式
- adb 启动报错
- ZrcListView
- Android中IntentService详解
- Android基础之IO流