一般人会说,就是用那个MBProgressHUD不就行了吗?

的确是,MBProgressHUD能为我们做了好多loading指示器,但是toast不一样,它是在屏幕某个地方出现一行小提示,例如网络状态,出现的时候不会妨碍用户其他点击操作,只是MBProgressHUD它本身设计出发点并不如此,所以我介绍一个比较独特的开源文件。

先看头文件

#import <Foundation/Foundation.h>#define kSGInfoAlert_fontSize       14
#define kSGInfoAlert_width          200
#define kMax_ConstrainedSize        CGSizeMake(250, 150)
#define kViewTag                    990@interface SGInfoAlert : UIView{CGColorRef bgcolor_;NSString *info_;CGSize fontSize_;
}// info为提示信息,frame为提示框大小,view是为消息框的superView(推荐Tabbarcontroller.view)
// vertical 为垂直方向上出现的位置 从 取值 0 ~ 1。
+ (SGInfoAlert *)showInfo:(NSString*)infobgColor:(CGColorRef)colorinView:(UIView*)view vertical:(float)height;@end

再看.m文件

#import "SGInfoAlert.h"@implementation SGInfoAlert// 画出圆角矩形背景
static void addRoundedRectToPath(CGContextRef context, CGRect rect,float ovalWidth,float ovalHeight)
{float fw, fh;if (ovalWidth == 0 || ovalHeight == 0) { CGContextAddRect(context, rect);return;}CGContextSaveGState(context); CGContextTranslateCTM (context, CGRectGetMinX(rect), CGRectGetMinY(rect));CGContextScaleCTM (context, ovalWidth, ovalHeight); fw = CGRectGetWidth (rect) / ovalWidth; fh = CGRectGetHeight (rect) / ovalHeight; CGContextMoveToPoint(context, fw, fh/2); CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1); CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1); CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1); CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); CGContextClosePath(context); CGContextRestoreGState(context);
}- (id)initWithFrame:(CGRect)frame bgColor:(CGColorRef)color info:(NSString*)info{CGRect viewR = CGRectMake(0, 0, frame.size.width*1.2, frame.size.height*1.2);self = [super initWithFrame:viewR];if (self) {self.backgroundColor = [UIColor clearColor];bgcolor_ = color;info_ = [[NSString alloc] initWithString:info];fontSize_ = frame.size;}return self;
}- (void)drawRect:(CGRect)rect{CGContextRef context = UIGraphicsGetCurrentContext();// 背景0.8透明度CGContextSetAlpha(context, .8);addRoundedRectToPath(context, rect, 4.0f, 4.0f);CGContextSetFillColorWithColor(context, bgcolor_);CGContextFillPath(context);// 文字1.0透明度CGContextSetAlpha(context, 1.0);
//    CGContextSetShadowWithColor(context, CGSizeMake(0, -1), 1, [[UIColor whiteColor] CGColor]);
    CGContextSetFillColorWithColor(context, [UIColor flatPurpleColor].CGColor);float x = (rect.size.width - fontSize_.width) / 2.0;float y = (rect.size.height - fontSize_.height) / 2.0;CGRect r = CGRectMake(x, y, fontSize_.width, fontSize_.height);[info_ drawInRect:r withFont:[UIFont systemFontOfSize:kSGInfoAlert_fontSize] lineBreakMode:UILineBreakModeTailTruncation];
}//- (void)dealloc{
//    [info_ release];
//    [super dealloc];
//}// 从上层视图移除并释放
- (void)remove{[self removeFromSuperview];
}// 渐变消失
- (void)fadeAway{[UIView beginAnimations:nil context:nil];[UIView setAnimationDuration:1.5f];self.alpha = .0;[UIView commitAnimations];[self performSelector:@selector(remove) withObject:nil afterDelay:1.5f];
}+ (SGInfoAlert *)showInfo:(NSString *)infobgColor:(CGColorRef)colorinView:(UIView *)view vertical:(float)height{if ([view viewWithTag:kViewTag] != nil) {SGInfoAlert *alert = (SGInfoAlert *)[view viewWithTag:kViewTag];[alert remove];}height = height < 0 ? 0 : height > 1 ? 1 : height;CGSize size = [info sizeWithFont:[UIFont systemFontOfSize:kSGInfoAlert_fontSize]constrainedToSize:kMax_ConstrainedSize];CGRect frame = CGRectMake(0, 0, size.width, size.height);SGInfoAlert *alert = [[SGInfoAlert alloc] initWithFrame:frame bgColor:color info:info];alert.center = CGPointMake(view.center.x, view.frame.size.height*height);alert.alpha = 0;[view addSubview:alert];alert.tag = kViewTag;
//    [alert release];
    [UIView beginAnimations:nil context:nil];[UIView setAnimationDuration:.3f];alert.alpha = 1.0;[UIView commitAnimations];
//    [alert performSelector:@selector(fadeAway) withObject:nil afterDelay:3.5];return alert;
}

我注销了一些地方,使得它可以在ARC环境下使用

转载于:https://www.cnblogs.com/nathanou/p/3273058.html

iOS下的类似Android的toast提示相关推荐

  1. web 弹出框 类似 android的toast的信息提示

    web js的alert弹出框,有时候并不是我们想要的,但是有时候又想给用户友好的提示,看了android的toast提示框也可在web前端实现,下面是参考一个人的源码稍作修改做的 // JavaSc ...

  2. 【转】iOS类似Android上toast效果

    原文网址:http://m.blog.csdn.net/article/details?id=50478737 做过Android开发的人都知道toast,它会在界面上显示一排黑色背景的文字,用于提示 ...

  3. ios手机怎么连接adb命令_没有 mac 的福音,windows 下对 ios 进行操作 (类似 android 的 adb 操作)...

    第一步 Download Quamotion iMobileDevice for Windows(地址:http://docs.quamotion.mobi/en/latest/imobiledevi ...

  4. Android仿Ios下拉回弹,Android ReboundScrollView仿IOS拖拽回弹效果

    初衷: 其实github上有很多这种ScrollView的项目,但是不得不说功能太多太乱了,我就只是想要一个简单效果的ScrollView,另外监听下滑动距离而已,想想还是自己写了个. 这里先说下思路 ...

  5. EditText控件的基本使用(点击Button按钮,Toast提示EditText中的内容)

    EditText是程序用于和用户进行交互的另一个重要控件,它允许用户在空间里输入和编辑内容,并可以在程序中对这些内容进行处理.EditText的应用场景非常普遍,在进行发短信.发微博.聊QQ等操作时, ...

  6. ofo 破解 android ios 版 (类似 wifi 万*能*钥*匙 )

    ofo 破解 android  ios 版   (类似   wifi 万*能*钥*匙   ) 使用 七牛云 做一个 破解ofo 的 大数据存储平台 举例说明 http://oi5lgyv8t.bkt. ...

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

    今天小年,再分享一篇2018年度最后一篇博客,博主是以iOS开发出身,那就最后一篇博文就分享一下关于iOS的内容吧.iOS开发过程中,有些时候操作App的时候,需要给用户对应的响应提示操作,使用系统自 ...

  8. android封装全局调用的toast_Android Toast提示封装实例代码

    Android Toast提示封装 Android中经常用到Toast提示,项目中很多Toast提示,写很长的一行,简单的封装一下,将Toast方法提出来,很方便使用: 实例代码: /** * 提示字 ...

  9. TensorFlow自带例子已经包含了android和ios下的摄像头图像分类示例Inception v1,这里补充一个Windows下的,使用AForge库(www.aforgenet.com)操作

    TensorFlow自带例子已经包含了android和ios下的摄像头图像分类示例Inception v1,这里补充一个Windows下的,使用AForge库(www.aforgenet.com)操作 ...

最新文章

  1. 回到未来 – 大胆畅想如何追赶并超越腾讯模式
  2. gin 获取post请求的json body操作详解
  3. 一阶和二阶微分方程的物理意义???
  4. 怎样获取网站的域名_搭建一个网站,通常的6大步骤你知道吗?
  5. 微信序列号生成器架构设计及演变
  6. nfs服务器_Kubernetes集群下部署NFS持久存储
  7. 2019年最流行的七大编程语言:学习编程,你会选择哪一种语言呢?
  8. 台风路径超级计算机,厄尔尼诺又要来了?2号台风或要生成,超级计算机:路径争议大...
  9. Python批量提取PowerPoint文件中所有幻灯片标题和备注文本
  10. sysbench数据库性能压测详解
  11. 【高数复盘】1.1映射与函数思维导图
  12. 使用eeupdate修改机器网卡mac地址
  13. freeimage 安装错误
  14. wps折线图如何画多条折线_如何用wps制作折线图
  15. php 秒拍视频解析,新浪微博视频解析源码_秒拍视频站外调用可自定义广告
  16. Miyeok Guk (Korean Seaweed Soup) - Миён Гук (Корейский суп из морепродуктов)
  17. 短视频直播平台第三方特效SDK接入教程(完整版)
  18. HTML5期末大作业:仿唯品会购物网站设计——仿唯品会购物商城(5页) HTML+CSS+JavaScript 学生DW网页设计作业成品 商城网站设计
  19. studio 3T连接不上mongoDB
  20. 晨枫U盘维护工具V2.0版(转)

热门文章

  1. 深入理解PHP原理之变量作用域
  2. linux中的apachectl是什么命令
  3. VS.Net中程序集的Debug版本和Release版本的区别
  4. 为什么栈和堆的生长方向不一样
  5. 一种注册表沙箱的思路、实现——注册表的一些基础知识
  6. windows7 64位机上安装配置CUDA7.5(或8.0)+cudnn5.0操作步骤
  7. 【C++】C++11 STL算法(七):排列操作(Permutation operations)、数值操作(Numeric operations)
  8. 【数据库】MySQL的C语言接口学习
  9. python error_python error整理
  10. python字符串按长度分割_python 按照固定长度分割字符串的方法小结