iOS开发:简单的Toast提示框实现

前言

博主是以iOS开发出身,那就最后一篇博文就分享一下关于iOS的内容吧。iOS开发过程中,有些时候操作App的时候,需要给用户对应的响应提示操作,使用系统自带的提示框不是每种情况都适用的。

除了使用系统自带的提示框,还有别的提示框选项,比如Toast提示框,Toast提示框在OC里面的使用效果就是仿制Android中的Toast的提示框效果,Toast弹出提示框显示文本,Toast会根据开发人员设置的显示时间后自动消失,使用起来方便简单。具体步骤如下所示:

一、新建一个工具类

首先新建一个基于NSObject的工具类,命名为JSToastDialogs,然后在这个工具类里面实现具体操作,具体代码如下所示:

1、.h文件

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>

//声明定义一个DialogsLabel对象

@interface DialogsLabel : UILabel

- (void)setMessageText:(NSString *)text;
@end

@interface JSToastDialogs : NSObject {

DialogsLabel *dialogsLabel;

NSTimer *countTimer;

}

//创建声明单例方法

+ (instancetype)shareInstance;

- (void)makeToast:(NSString *)message duration:(CGFloat)duration;

@end

2、.m文件

#import "JSToastDialogs.h"

static int changeCount;

@implementation JSToastDialogs

// 实现声明单例方法 GCD

+ (instancetype)shareInstance {

static JSToastDialogs *singleton = nil;

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

singleton = [[JSToastDialogs alloc] init];

});

return singleton;
}

// 初始化方法

- (instancetype)init {

self = [super init];

if (self) {

dialogsLabel = [[DialogsLabel alloc]init];

countTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeTime) userInfo:nil repeats:YES];

countTimer.fireDate = [NSDate distantFuture];//关闭定时器

}

return self;

}



/**

* 弹出并显示JSToastDialogs

* @param message 显示的文本内容

* @param duration 显示时间

*/

- (void)makeToast:(NSString *)message duration:(CGFloat)duration {

if ([message length] == 0) {

return;

}

[dialogsLabel setMessageText:message];

[[[UIApplication sharedApplication] keyWindow] addSubview:dialogsLabel];

dialogsLabel.alpha = 0.8;

countTimer.fireDate = [NSDate distantPast];//开启定时器

changeCount = duration;

}



//定时器回调方法

- (void)changeTime {

if(changeCount-- <= 0){

countTimer.fireDate = [NSDate distantFuture]; //关闭定时器

[UIView animateWithDuration:0.2f animations:^{

dialogsLabel.alpha = 0;

} completion:^(BOOL finished) {

[dialogsLabel removeFromSuperview];

}];

}

}

@end



#pragma mark - DialogsLabel的方法

@implementation DialogsLabel

//DialogsLabel初始化,为label设置各种属性

- (instancetype)init {

self = [super init];

if (self) {

self.layer.cornerRadius = 8;

self.layer.masksToBounds = YES;

self.backgroundColor = [UIColor blackColor];

self.numberOfLines = 0;

self.textAlignment = NSTextAlignmentCenter;

self.textColor = [UIColor whiteColor];

self.font = [UIFont systemFontOfSize:15];

}

return self;

}

//设置显示的文字

- (void)setMessageText:(NSString *)text {

[self setText:text];

CGRect rect = [self.text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:self.font} context:nil];

CGFloat width = rect.size.width + 20;

CGFloat height = rect.size.height + 20;

CGFloat x = (SCREEN_WIDTH-width)/2;

CGFloat y = SCREEN_HEIGHT-height - 59;

self.frame = CGRectMake(x, y, width, height);

}

@end

二、在控制器中的使用

在具体需要使用的地方,首先要导入工具类的头文件,或者把头文件放到.pch文件里面,这样可以一劳永逸,随时随地在项目任何地方都可以使用。具体的使用过程如下所示:

- (void)writeDataComplate:(BOOL)doSuc {

if (doSuc) {

//发送成功 弹框显示

[[JSToastDialogs shareInstance] makeToast:@"指令发送成功!" duration:1.0];

}else {

//发送失败 弹框显示

[[JSToastDialogs shareInstance] makeToast:@"error!" duration:1.0];

}

}

具体效果如下所示:

iOS开发:简单的Toast提示框实现

最后

以上就是在iOS开发中简单的Toast提示框实现用法总结的知识点,由于是常用的知识点,所以没有涵盖所有的属性,没有涉及到的属性请读者自行查阅官方API,这里就不再赘述。以上就是本章全部内容,欢迎关注三掌柜的微信公众号“程序猿by三掌柜”,三掌柜的新浪微博“三掌柜666”,欢迎关注!

文章版权声明

 1 原创文章作者:4701,如若转载,请注明出处: https://www.52hwl.com/37481.html

 2 温馨提示:软件侵权请联系469472785#qq.com(三天内删除相关链接)资源失效请留言反馈

 3 下载提示:如遇蓝奏云无法访问,请修改lanzous(把s修改成x)

 免责声明:本站为个人博客,所有软件信息均来自网络 修改版软件,加群广告提示为修改者自留,非本站信息,注意鉴别

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023年7月15日 上午9:07
下一篇 2023年7月15日 上午9:07