iOS开发之类似安卓的Toast短暂提示框
来源:互联网 发布:俄罗斯 共青城 知乎 编辑:程序博客网 时间:2024/06/05 17:34
向凡神致敬~
在Android中具有确认提示框与短暂提示框Toast,但在iOS中只有确认提示框Alert并无类似于Android的短暂提示框Toast。
注:使用此组件控制器需加入到UINavigationController中
使用方法:
- [[[ZFToast alloc] init] popUpToastWithMessage:@"提示内容"];
源码:ZFToast.h文件
-
-
-
-
-
- #import <UIKit/UIKit.h>
-
-
-
-
-
-
-
-
-
-
-
-
-
- @interface ZFToast : UIView
-
-
- - (void)popUpToastWithMessage:(NSString *)message;
-
- @end</span><span style="color:#cc0000;">
- </span>
ZFToast.m文件- import "ZFToast.h"
-
- @interface ZFToast ()
-
-
- @property (strong,nonatomic) UILabel *textLabel;
- @property (strong,nonatomic) ZFToast *toast;
- @property (strong,nonatomic) NSTimer *timer;
-
- @property (assign,nonatomic) NSInteger currentDate;
-
- @end
- @implementation ZFToast
-
- - (instancetype)init
- {
- self = [super init];
- if (self) {
- self.currentDate = 0;
- }
-
- return self;
- }
-
- - (void)popUpToastWithMessage:(NSString *)message
- {
-
- [self createTimer];
-
- [self initLabel:message];
-
- [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]);
-
- UIFont *font = [UIFont systemFontOfSize:15];
-
- CGFloat width = screenWidth / 3.0 * 2.0;
-
- 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];
-
- 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:563699115
Telephone:18341266547
1 0