iOS开发:Toast for iPhone
iOS开发:Toast for iPhone
主要特点:
1,支持屏幕Y轴任意位置显示,设置距离顶/底端距离
2,支持多行文本
3,支持设置等待时间
4,支持点击隐藏,屏幕旋转时自动隐藏,淡入淡出
5,无需初始化,类方法调用
效果图:

全部代码如下,使用时需要添加QuartzCore.framework,希望能给大家带来方便。
- #import <Foundation/Foundation.h>
- #define DEFAULT_DISPLAY_DURATION 2.0f
- @interface OMGToast : NSObject {
- NSString *text;
- UIButton *contentView;
- CGFloat duration;
- }
- + (void)showWithText:(NSString *) text_;
- + (void)showWithText:(NSString *) text_
- duration:(CGFloat)duration_;
- + (void)showWithText:(NSString *) text_
- topOffset:(CGFloat) topOffset_;
- + (void)showWithText:(NSString *) text_
- topOffset:(CGFloat) topOffset
- duration:(CGFloat) duration_;
- + (void)showWithText:(NSString *) text_
- bottomOffset:(CGFloat) bottomOffset_;
- + (void)showWithText:(NSString *) text_
- bottomOffset:(CGFloat) bottomOffset_
- duration:(CGFloat) duration_;
- @end
- #import "OMGToast.h"
- #import <QuartzCore/QuartzCore.h>
- @interface OMGToast (private)
- - (id)initWithText:(NSString *)text_;
- - (void)setDuration:(CGFloat) duration_;
- - (void)dismisToast;
- - (void)toastTaped:(UIButton *)sender_;
- - (void)showAnimation;
- - (void)hideAnimation;
- - (void)show;
- - (void)showFromTopOffset:(CGFloat) topOffset_;
- - (void)showFromBottomOffset:(CGFloat) bottomOffset_;
- @end
- @implementation OMGToast
- - (void)dealloc{
- [[NSNotificationCenter defaultCenter] removeObserver:self
- name:UIDeviceOrientationDidChangeNotification
- object:[UIDevice currentDevice]];
- [contentView release],contentView = nil;
- [text release],text = nil;
- [super dealloc];
- }
- - (id)initWithText:(NSString *)text_{
- if (self = [super init]) {
- text = [text_ copy];
- UIFont *font = [UIFont boldSystemFontOfSize:14];
- CGSize textSize = [text sizeWithFont:font
- constrainedToSize:CGSizeMake(280, MAXFLOAT)
- lineBreakMode:UILineBreakModeWordWrap];
- UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, textSize.width + 12, textSize.height + 12)];
- textLabel.backgroundColor = [UIColor clearColor];
- textLabel.textColor = [UIColor whiteColor];
- textLabel.textAlignment = UITextAlignmentCenter;
- textLabel.font = font;
- textLabel.text = text;
- textLabel.numberOfLines = 0;
- contentView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, textLabel.frame.size.width, textLabel.frame.size.height)];
- contentView.layer.cornerRadius = 5.0f;
- contentView.layer.borderWidth = 1.0f;
- contentView.layer.borderColor = [[UIColor grayColor] colorWithAlphaComponent:0.5].CGColor;
- contentView.backgroundColor = [UIColor colorWithRed:0.2f
- green:0.2f
- blue:0.2f
- alpha:0.75f];
- [contentView addSubview:textLabel];
- contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
- [contentView addTarget:self
- action:@selector(toastTaped:)
- forControlEvents:UIControlEventTouchDown];
- contentView.alpha = 0.0f;
- [textLabel release];
- duration = DEFAULT_DISPLAY_DURATION;
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(deviceOrientationDidChanged:)
- name:UIDeviceOrientationDidChangeNotification
- object:[UIDevice currentDevice]];
- }
- return self;
- }
- - (void)deviceOrientationDidChanged:(NSNotification *)notify_{
- [self hideAnimation];
- }
- -(void)dismissToast{
- [contentView removeFromSuperview];
- }
- -(void)toastTaped:(UIButton *)sender_{
- [self hideAnimation];
- }
- - (void)setDuration:(CGFloat) duration_{
- duration = duration_;
- }
- -(void)showAnimation{
- [UIView beginAnimations:@"show" context:NULL];
- [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
- [UIView setAnimationDuration:0.3];
- contentView.alpha = 1.0f;
- [UIView commitAnimations];
- }
- -(void)hideAnimation{
- [UIView beginAnimations:@"hide" context:NULL];
- [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
- [UIView setAnimationDelegate:self];
- [UIView setAnimationDidStopSelector:@selector(dismissToast)];
- [UIView setAnimationDuration:0.3];
- contentView.alpha = 0.0f;
- [UIView commitAnimations];
- }
- - (void)show{
- UIWindow *window = [UIApplication sharedApplication].keyWindow;
- contentView.center = window.center;
- [window addSubview:contentView];
- [self showAnimation];
- [self performSelector:@selector(hideAnimation) withObject:nil afterDelay:duration];
- }
- - (void)showFromTopOffset:(CGFloat) top_{
- UIWindow *window = [UIApplication sharedApplication].keyWindow;
- contentView.center = CGPointMake(window.center.x, top_ + contentView.frame.size.height/2);
- [window addSubview:contentView];
- [self showAnimation];
- [self performSelector:@selector(hideAnimation) withObject:nil afterDelay:duration];
- }
- - (void)showFromBottomOffset:(CGFloat) bottom_{
- UIWindow *window = [UIApplication sharedApplication].keyWindow;
- contentView.center = CGPointMake(window.center.x, window.frame.size.height-(bottom_ + contentView.frame.size.height/2));
- [window addSubview:contentView];
- [self showAnimation];
- [self performSelector:@selector(hideAnimation) withObject:nil afterDelay:duration];
- }
- + (void)showWithText:(NSString *)text_{
- [OMGToast showWithText:text_ duration:DEFAULT_DISPLAY_DURATION];
- }
- + (void)showWithText:(NSString *)text_
- duration:(CGFloat)duration_{
- OMGToast *toast = [[[OMGToast alloc] initWithText:text_] autorelease];
- [toast setDuration:duration_];
- [toast show];
- }
- + (void)showWithText:(NSString *)text_
- topOffset:(CGFloat)topOffset_{
- [OMGToast showWithText:text_ topOffset:topOffset_ duration:DEFAULT_DISPLAY_DURATION];
- }
- + (void)showWithText:(NSString *)text_
- topOffset:(CGFloat)topOffset_
- duration:(CGFloat)duration_{
- OMGToast *toast = [[[OMGToast alloc] initWithText:text_] autorelease];
- [toast setDuration:duration_];
- [toast showFromTopOffset:topOffset_];
- }
- + (void)showWithText:(NSString *)text_
- bottomOffset:(CGFloat)bottomOffset_{
- [OMGToast showWithText:text_ bottomOffset:bottomOffset_ duration:DEFAULT_DISPLAY_DURATION];
- }
- + (void)showWithText:(NSString *)text_
- bottomOffset:(CGFloat)bottomOffset_
- duration:(CGFloat)duration_{
- OMGToast *toast = [[[OMGToast alloc] initWithText:text_] autorelease];
- [toast setDuration:duration_];
- [toast showFromBottomOffset:bottomOffset_];
- }
- @end
可以这样来使用
- [OMGToast showWithText:@"中间显示" duration:5];
- [OMGToast showWithText:@"距离上方50像素" topOffset:50 duration:5];
- [OMGToast showWithText:@"文字很多的时候,我就会自动折行,最大宽度280像素" topOffset:100 duration:5];
- [OMGToast showWithText:@"加入\\n也可以\n显示\n多\n行" topOffset:300 duration:5];
- [OMGToast showWithText:@"距离下方3像素" bottomOffset:3 duration:5];
iOS开发:Toast for iPhone的更多相关文章
- iOS开发点滴:iPhone屏幕适配
最近开始做iOS开发,遇到一些小问题和解决方法,记录下. 今天是iPhone屏幕适配 iPhone5出来之后屏幕就有iPhone就有了2种尺寸:3.5寸和4寸,xcode 5 的IB设计器里面界面 ...
- iOS开发:判断iPhone是否是刘海屏iPhoneX、iPhoneXR、iPhoneXs、iPhoneXs Max等
保证能判断,呕心沥血,不行切JIJI 方法一 Objective-C // iPhoneX.iPhoneXR.iPhoneXs.iPhoneXs Max等 // 判断刘海屏,返回YES表示是刘海屏 - ...
- ios开发介绍
iOS开发概述 •什么是IOS •什么是IOS开发 •为什么要选择IOS开发 •学习IOS开发的准备 1.什么是iOS •iOS是一款由苹果公司开发的操作系统(OS是Operating Sys ...
- ios新手开发——toast提示和旋转图片加载框
不知不觉自学ios已经四个月了,从OC语法到app开发,过程虽然枯燥无味,但是结果还是挺有成就感的,在此分享我的ios开发之路中的小小心得~废话不多说,先上我们今天要实现的效果图: 有过一点做APP经 ...
- iOS开发UI篇—iPad和iPhone开发的比较
一.iPad简介 1.什么是iPad 一款苹果公司于2010年发布的平板电脑 定位介于苹果的智能手机iPhone和笔记本电脑产品之间 跟iPhone一样,搭载的是iOS操作系统 2.iPad的市场情况 ...
- iOS开发---iPhone SDK 包含哪些东西?
第一部分: 在使用Intel芯片的Macintosh计算机开发iOS应用程序所需的全部接口.工具以及资源全都包含于iPhone SDK. 苹果公司将大部分系统接口发布在框架这种特殊的数据包.一个框架就 ...
- IOS开发/iphone开发多线程
有时候可能有很多功能要同时实现,例如每隔多长时间就会检测程序网络连接,又或者有时候需要从服务器下载一个不小的文件,如果用单线程几乎是不可想的事情,程序将会卡的无法使用,用到多线程和不用多线程,给用户的 ...
- 【转】iOS开发UI篇—iPad和iPhone开发的比较
原文网址:http://www.cnblogs.com/wendingding/p/3918007.html iOS开发UI篇—iPad和iPhone开发的比较 一.iPad简介 1.什么是iPad ...
- IOS开发系列之阿堂教程:玩转IPhone客户端和Web服务端交互(客户端)实践
说到ios的应用开发,我们不能不提到web server服务端,如果没有服务端的支持,ios应用开发就没有多大意义了,因为从事过手机开发的朋友都知道(Android也一样),大量复杂业务的处理和数据库 ...
随机推荐
- fish shell安装和配置
sudo apt-get install fish whereis fish chsh -s /usr/bin/fish 重启:
- WIFI万能钥匙协议分析
WIFI万能钥匙协议分析 需求: 上android 市场下载任意一款,wifi万能钥匙 软件,对其进行 协议分析和逆向,达成如下结果:通过对软件的分析,完成自动化爬虫,爬wifi万能钥匙的wifi库, ...
- Miracast HDCP 等知识
Miracast 通讯架构中关于视频数据处理流程的部分.整个视频数据处理及传输的流程,大致上分为几个阶段,一开始将撷取到系统的画面及声音进行压缩,而压缩后的影音数据再转为基本封包串流(Packetiz ...
- java web项目防止多用户重复登录解决方案
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本人声明.否则将追究法律责任.作者:永恒の_☆ 地址:http://blog.csdn.net/chenghui031 ...
- 洛谷P1101 单词方阵
题目描述 给一nXn的字母方阵,内可能蕴含多个“yizhong”单词.单词在方阵中是沿着同一方向连续摆放的.摆放可沿着8个方向的任一方向,同一单词摆放时不再改变方向,单词与单词之间[color=red ...
- 一个老忘且非常有用的jquery动画方法 网页上卷
$('html,body').animate({scrollTop:800+'px'},500) //网页上卷800像素 在半秒之内
- XSD(XML Schema Definition)学习笔记
今天学习了XSD相关的知识,为了以后查找的方便,写一些笔记. 一.什么是XSD? 1.XSD全称:XML Schema Definition.XML Schema 的作用是定义 XML 文档的合法构建 ...
- hdu 5455(字符串处理)
Fang Fang Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total S ...
- 用jQuery File Upload实现简单的文件上传
FORM中的代码: {# file_path #} <div class="form-group"> <label class="control-lab ...
- js-判断移动端用户是横屏放的还是竖屏放的
在一些移动端页面中,页面的样式我们就支持竖屏播放,这时我们就需要判断是横屏还是竖屏了,不同的方向显示冉的样式. //判断手机横屏竖屏 var html = ""; window.a ...