iOS开发:Toast for iPhone

 
分享一个我写的类似于android的toast的提示框

主要特点:

1,支持屏幕Y轴任意位置显示,设置距离顶/底端距离

2,支持多行文本

3,支持设置等待时间

4,支持点击隐藏,屏幕旋转时自动隐藏,淡入淡出

5,无需初始化,类方法调用

效果图:

全部代码如下,使用时需要添加QuartzCore.framework,希望能给大家带来方便。

  1. #import <Foundation/Foundation.h>
  2. #define DEFAULT_DISPLAY_DURATION 2.0f
  3. @interface OMGToast : NSObject {
  4. NSString *text;
  5. UIButton *contentView;
  6. CGFloat  duration;
  7. }
  8. + (void)showWithText:(NSString *) text_;
  9. + (void)showWithText:(NSString *) text_
  10. duration:(CGFloat)duration_;
  11. + (void)showWithText:(NSString *) text_
  12. topOffset:(CGFloat) topOffset_;
  13. + (void)showWithText:(NSString *) text_
  14. topOffset:(CGFloat) topOffset
  15. duration:(CGFloat) duration_;
  16. + (void)showWithText:(NSString *) text_
  17. bottomOffset:(CGFloat) bottomOffset_;
  18. + (void)showWithText:(NSString *) text_
  19. bottomOffset:(CGFloat) bottomOffset_
  20. duration:(CGFloat) duration_;
  21. @end
  1. #import "OMGToast.h"
  2. #import <QuartzCore/QuartzCore.h>
  3. @interface OMGToast (private)
  4. - (id)initWithText:(NSString *)text_;
  5. - (void)setDuration:(CGFloat) duration_;
  6. - (void)dismisToast;
  7. - (void)toastTaped:(UIButton *)sender_;
  8. - (void)showAnimation;
  9. - (void)hideAnimation;
  10. - (void)show;
  11. - (void)showFromTopOffset:(CGFloat) topOffset_;
  12. - (void)showFromBottomOffset:(CGFloat) bottomOffset_;
  13. @end
  14. @implementation OMGToast
  15. - (void)dealloc{
  16. [[NSNotificationCenter defaultCenter] removeObserver:self
  17. name:UIDeviceOrientationDidChangeNotification
  18. object:[UIDevice currentDevice]];
  19. [contentView release],contentView = nil;
  20. [text release],text = nil;
  21. [super dealloc];
  22. }
  23. - (id)initWithText:(NSString *)text_{
  24. if (self = [super init]) {
  25. text = [text_ copy];
  26. UIFont *font = [UIFont boldSystemFontOfSize:14];
  27. CGSize textSize = [text sizeWithFont:font
  28. constrainedToSize:CGSizeMake(280, MAXFLOAT)
  29. lineBreakMode:UILineBreakModeWordWrap];
  30. UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, textSize.width + 12, textSize.height + 12)];
  31. textLabel.backgroundColor = [UIColor clearColor];
  32. textLabel.textColor = [UIColor whiteColor];
  33. textLabel.textAlignment = UITextAlignmentCenter;
  34. textLabel.font = font;
  35. textLabel.text = text;
  36. textLabel.numberOfLines = 0;
  37. contentView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, textLabel.frame.size.width, textLabel.frame.size.height)];
  38. contentView.layer.cornerRadius = 5.0f;
  39. contentView.layer.borderWidth = 1.0f;
  40. contentView.layer.borderColor = [[UIColor grayColor] colorWithAlphaComponent:0.5].CGColor;
  41. contentView.backgroundColor = [UIColor colorWithRed:0.2f
  42. green:0.2f
  43. blue:0.2f
  44. alpha:0.75f];
  45. [contentView addSubview:textLabel];
  46. contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  47. [contentView addTarget:self
  48. action:@selector(toastTaped:)
  49. forControlEvents:UIControlEventTouchDown];
  50. contentView.alpha = 0.0f;
  51. [textLabel release];
  52. duration = DEFAULT_DISPLAY_DURATION;
  53. [[NSNotificationCenter defaultCenter] addObserver:self
  54. selector:@selector(deviceOrientationDidChanged:)
  55. name:UIDeviceOrientationDidChangeNotification
  56. object:[UIDevice currentDevice]];
  57. }
  58. return self;
  59. }
  60. - (void)deviceOrientationDidChanged:(NSNotification *)notify_{
  61. [self hideAnimation];
  62. }
  63. -(void)dismissToast{
  64. [contentView removeFromSuperview];
  65. }
  66. -(void)toastTaped:(UIButton *)sender_{
  67. [self hideAnimation];
  68. }
  69. - (void)setDuration:(CGFloat) duration_{
  70. duration = duration_;
  71. }
  72. -(void)showAnimation{
  73. [UIView beginAnimations:@"show" context:NULL];
  74. [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
  75. [UIView setAnimationDuration:0.3];
  76. contentView.alpha = 1.0f;
  77. [UIView commitAnimations];
  78. }
  79. -(void)hideAnimation{
  80. [UIView beginAnimations:@"hide" context:NULL];
  81. [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
  82. [UIView setAnimationDelegate:self];
  83. [UIView setAnimationDidStopSelector:@selector(dismissToast)];
  84. [UIView setAnimationDuration:0.3];
  85. contentView.alpha = 0.0f;
  86. [UIView commitAnimations];
  87. }
  88. - (void)show{
  89. UIWindow *window = [UIApplication sharedApplication].keyWindow;
  90. contentView.center = window.center;
  91. [window  addSubview:contentView];
  92. [self showAnimation];
  93. [self performSelector:@selector(hideAnimation) withObject:nil afterDelay:duration];
  94. }
  95. - (void)showFromTopOffset:(CGFloat) top_{
  96. UIWindow *window = [UIApplication sharedApplication].keyWindow;
  97. contentView.center = CGPointMake(window.center.x, top_ + contentView.frame.size.height/2);
  98. [window  addSubview:contentView];
  99. [self showAnimation];
  100. [self performSelector:@selector(hideAnimation) withObject:nil afterDelay:duration];
  101. }
  102. - (void)showFromBottomOffset:(CGFloat) bottom_{
  103. UIWindow *window = [UIApplication sharedApplication].keyWindow;
  104. contentView.center = CGPointMake(window.center.x, window.frame.size.height-(bottom_ + contentView.frame.size.height/2));
  105. [window  addSubview:contentView];
  106. [self showAnimation];
  107. [self performSelector:@selector(hideAnimation) withObject:nil afterDelay:duration];
  108. }
  109. + (void)showWithText:(NSString *)text_{
  110. [OMGToast showWithText:text_ duration:DEFAULT_DISPLAY_DURATION];
  111. }
  112. + (void)showWithText:(NSString *)text_
  113. duration:(CGFloat)duration_{
  114. OMGToast *toast = [[[OMGToast alloc] initWithText:text_] autorelease];
  115. [toast setDuration:duration_];
  116. [toast show];
  117. }
  118. + (void)showWithText:(NSString *)text_
  119. topOffset:(CGFloat)topOffset_{
  120. [OMGToast showWithText:text_  topOffset:topOffset_ duration:DEFAULT_DISPLAY_DURATION];
  121. }
  122. + (void)showWithText:(NSString *)text_
  123. topOffset:(CGFloat)topOffset_
  124. duration:(CGFloat)duration_{
  125. OMGToast *toast = [[[OMGToast alloc] initWithText:text_] autorelease];
  126. [toast setDuration:duration_];
  127. [toast showFromTopOffset:topOffset_];
  128. }
  129. + (void)showWithText:(NSString *)text_
  130. bottomOffset:(CGFloat)bottomOffset_{
  131. [OMGToast showWithText:text_  bottomOffset:bottomOffset_ duration:DEFAULT_DISPLAY_DURATION];
  132. }
  133. + (void)showWithText:(NSString *)text_
  134. bottomOffset:(CGFloat)bottomOffset_
  135. duration:(CGFloat)duration_{
  136. OMGToast *toast = [[[OMGToast alloc] initWithText:text_] autorelease];
  137. [toast setDuration:duration_];
  138. [toast showFromBottomOffset:bottomOffset_];
  139. }
  140. @end

可以这样来使用

  1. [OMGToast showWithText:@"中间显示" duration:5];
  2. [OMGToast showWithText:@"距离上方50像素" topOffset:50 duration:5];
  3. [OMGToast showWithText:@"文字很多的时候,我就会自动折行,最大宽度280像素" topOffset:100 duration:5];
  4. [OMGToast showWithText:@"加入\\n也可以\n显示\n多\n行" topOffset:300 duration:5];
  5. [OMGToast showWithText:@"距离下方3像素" bottomOffset:3 duration:5];
 
 
 

iOS开发:Toast for iPhone的更多相关文章

  1. iOS开发点滴:iPhone屏幕适配

    最近开始做iOS开发,遇到一些小问题和解决方法,记录下.   今天是iPhone屏幕适配 iPhone5出来之后屏幕就有iPhone就有了2种尺寸:3.5寸和4寸,xcode 5 的IB设计器里面界面 ...

  2. iOS开发:判断iPhone是否是刘海屏iPhoneX、iPhoneXR、iPhoneXs、iPhoneXs Max等

    保证能判断,呕心沥血,不行切JIJI 方法一 Objective-C // iPhoneX.iPhoneXR.iPhoneXs.iPhoneXs Max等 // 判断刘海屏,返回YES表示是刘海屏 - ...

  3. ios开发介绍

    iOS开发概述 •什么是IOS •什么是IOS开发 •为什么要选择IOS开发 •学习IOS开发的准备   1.什么是iOS   •iOS是一款由苹果公司开发的操作系统(OS是Operating Sys ...

  4. ios新手开发——toast提示和旋转图片加载框

    不知不觉自学ios已经四个月了,从OC语法到app开发,过程虽然枯燥无味,但是结果还是挺有成就感的,在此分享我的ios开发之路中的小小心得~废话不多说,先上我们今天要实现的效果图: 有过一点做APP经 ...

  5. iOS开发UI篇—iPad和iPhone开发的比较

    一.iPad简介 1.什么是iPad 一款苹果公司于2010年发布的平板电脑 定位介于苹果的智能手机iPhone和笔记本电脑产品之间 跟iPhone一样,搭载的是iOS操作系统 2.iPad的市场情况 ...

  6. iOS开发---iPhone SDK 包含哪些东西?

    第一部分: 在使用Intel芯片的Macintosh计算机开发iOS应用程序所需的全部接口.工具以及资源全都包含于iPhone SDK. 苹果公司将大部分系统接口发布在框架这种特殊的数据包.一个框架就 ...

  7. IOS开发/iphone开发多线程

    有时候可能有很多功能要同时实现,例如每隔多长时间就会检测程序网络连接,又或者有时候需要从服务器下载一个不小的文件,如果用单线程几乎是不可想的事情,程序将会卡的无法使用,用到多线程和不用多线程,给用户的 ...

  8. 【转】iOS开发UI篇—iPad和iPhone开发的比较

    原文网址:http://www.cnblogs.com/wendingding/p/3918007.html iOS开发UI篇—iPad和iPhone开发的比较 一.iPad简介 1.什么是iPad ...

  9. IOS开发系列之阿堂教程:玩转IPhone客户端和Web服务端交互(客户端)实践

    说到ios的应用开发,我们不能不提到web server服务端,如果没有服务端的支持,ios应用开发就没有多大意义了,因为从事过手机开发的朋友都知道(Android也一样),大量复杂业务的处理和数据库 ...

随机推荐

  1. [LOJ#530]「LibreOJ β Round #5」最小倍数

    [LOJ#530]「LibreOJ β Round #5」最小倍数 试题描述 第二天,LCR 终于启动了备份存储器,准备上传数据时,却没有找到熟悉的文件资源,取而代之的是而屏幕上显示的一段话: 您的文 ...

  2. spring的事务传播与隔离

    propagation 事务的传播属性: 1.PROPAGATION_REQUIRED(*-required):支持当前事务,如果当前没有事务,就新建一个事务.(最常见的选择) 2.PROPAGATI ...

  3. APUE 学习笔记(六) 进程控制

    1. fork 创建新进程 fork创建的新进程称为子进程,fork函数调用一次,返回两次. 两次返回的唯一区别就是子进程的返回值是0,而父进程的返回值是新子进程的进程ID 在fork之后是父进程先执 ...

  4. poj 4001 To Miss Our Children Time

    To Miss Our Children Time Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Jav ...

  5. mysql 新增用户并授权

    grant all privileges on *.* to 'root'@‘%’ identified by '123456'; *.* 表示所有资源. 刷新权限 flush privileges;

  6. 阿里云oss教程

    OSS是提供非结构化数据存取的服务.对于刚开始使用OSS的用户,非结构数据可以理解为word文档.PDF.PPT.EXCEL表格.MP3.MKV.RMVB.HTML等各种类型文件.OSS提供API去进 ...

  7. PAT 甲级 1045 Favorite Color Stripe(DP)

    题目链接 Favorite Color Stripe 题意:给定$A$序列和$B$序列,你需要在$B$序列中找出任意一个最长的子序列,使得这个子序列也是$A$的子序列 (这个子序列的相邻元素可以重复) ...

  8. ros pub sub ("~")

    base_velocity_smoother.cpp                        remap ("~")    订阅: "cmd_vel"   ...

  9. poj 2104 K-th Number(主席树

    Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 59058   Accepted: 20529 Case Time Limi ...

  10. 【UTR #2】题目交流通道

    题目描述 定好了难度,雄心勃勃的吉米多出题斯基开始寻找智慧的神犇星球的居民出题. 然而吉米多出题斯基没有料到,神犇星球的居民告诉吉米多出题斯基:"今年神犇星球经济不景气,大家都想宅在家里,哪 ...