首页 > 代码库 > iOS开发:Toast for iPhone

iOS开发:Toast for iPhone

 

iOS开发:Toast for iPhone 

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

 

主要特点:

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

2,支持多行文本

3,支持设置等待时间

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

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

 

效果图:

技术分享

 

 

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

 


 
 
  1. #import <Foundation/Foundation.h>  
  2.   
  3. #define DEFAULT_DISPLAY_DURATION 2.0f  
  4.   
  5. @interface OMGToast : NSObject {  
  6.     NSString *text;  
  7.     UIButton *contentView;  
  8.     CGFloat  duration;  
  9. }  
  10.   
  11. + (void)showWithText:(NSString *) text_;  
  12. + (void)showWithText:(NSString *) text_  
  13.             duration:(CGFloat)duration_;  
  14.   
  15. + (void)showWithText:(NSString *) text_  
  16.            topOffset:(CGFloat) topOffset_;  
  17. + (void)showWithText:(NSString *) text_  
  18.            topOffset:(CGFloat) topOffset  
  19.             duration:(CGFloat) duration_;  
  20.   
  21. + (void)showWithText:(NSString *) text_  
  22.         bottomOffset:(CGFloat) bottomOffset_;  
  23. + (void)showWithText:(NSString *) text_  
  24.         bottomOffset:(CGFloat) bottomOffset_  
  25.             duration:(CGFloat) duration_;  
  26.   
  27. @end  


 


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