关于iOS中类似于Android的Toast短暂提示框
来源:互联网 发布:部落冲突地狱塔数据 编辑:程序博客网 时间:2024/05/17 08:09
在Android中具有确认提示框与短暂提示框Toast,但在iOS中只有确认提示框Alert并无类似于Android的短暂提示框Toast,若需要可自己写一个,博主自己写了一个已上传至github,下载请点击
注:使用此组件控制器需加入到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
0 0
- 关于iOS中类似于Android的Toast短暂提示框
- ios 类似于android Toast
- ios 类似于android Toast
- iOS开发之类似安卓的Toast短暂提示框
- android中连续点击,Toast短暂弹出
- (ios实战)实现类似于android 的toast控件
- Android开发中提示框Toast、AlertDialog的使用!
- IOS 基于Swift的弹出提示框(仿Android的Toast 和等待提示框)
- IOS控件列---仿Android的Toast提示信息
- Android中关于Toast的使用体会
- Android自定义类似于QQ的消息提示框
- Android UI界面中短暂消息提示实现
- Android 的提示接口-Toast
- android提示Toast的使用
- Android中类似于IOS的CoverFlow效果实现源码分析
- Android中实现带声音提示的Toast
- toast(uiview) 类似于android的iostoast功能实现
- iOS 实现Android Toast消息提示 效果
- Objective-C学习中Cocoa提供的一种特性(KVC)
- org/apache/hadoop/hive/cli/CliDriver : Unsupported major.minor version 51.0
- cf#ECR10-D. Nested Segments-树状数组+二分
- POJ 1101 译文
- 第一篇:个人感悟----懒惰是人类最大的敌人
- 关于iOS中类似于Android的Toast短暂提示框
- java关键字总结
- ROS的安装与环境配置
- Gradle学习资料整理
- 异常信息定位具体数据行
- JVM(二)--Java代码编译和执行的过程
- jquery 自定义事件绑定与触发 $.one与$.bind效果对比实例
- 《亥时夜游湖滨道中》
- ios9新特性学习方法和运行时(RunTime)解析