天天看点

iOS开发:Toast for iPhone

iOS开发:Toast for iPhone 

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

主要特点:

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

2,支持多行文本

3,支持设置等待时间

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

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

效果图:

iOS开发:Toast for iPhone
全部代码如下,使用时需要添加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.            topOffset:(CGFloat) topOffset_;  
  12.            topOffset:(CGFloat) topOffset  
  13.             duration:(CGFloat) duration_;  
  14.         bottomOffset:(CGFloat) bottomOffset_;  
  15.         bottomOffset:(CGFloat) bottomOffset_  
  16. @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. @implementation OMGToast  
  14. - (void)dealloc{  
  15.     [[NSNotificationCenter defaultCenter] removeObserver:self  
  16.                                                     name:UIDeviceOrientationDidChangeNotification  
  17.                                                   object:[UIDevice currentDevice]];  
  18.     [contentView release],contentView = nil;  
  19.     [text release],text = nil;  
  20.     [super dealloc];      
  21. - (id)initWithText:(NSString *)text_{  
  22.     if (self = [super init]) {  
  23.         text = [text_ copy];  
  24.         UIFont *font = [UIFont boldSystemFontOfSize:14];  
  25.         CGSize textSize = [text sizeWithFont:font  
  26.                            constrainedToSize:CGSizeMake(280, MAXFLOAT)  
  27.                                lineBreakMode:UILineBreakModeWordWrap];  
  28.         UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, textSize.width + 12, textSize.height + 12)];  
  29.         textLabel.backgroundColor = [UIColor clearColor];  
  30.         textLabel.textColor = [UIColor whiteColor];  
  31.         textLabel.textAlignment = UITextAlignmentCenter;  
  32.         textLabel.font = font;  
  33.         textLabel.text = text;  
  34.         textLabel.numberOfLines = 0;  
  35.         contentView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, textLabel.frame.size.width, textLabel.frame.size.height)];  
  36.         contentView.layer.cornerRadius = 5.0f;  
  37.         contentView.layer.borderWidth = 1.0f;  
  38.         contentView.layer.borderColor = [[UIColor grayColor] colorWithAlphaComponent:0.5].CGColor;  
  39.         contentView.backgroundColor = [UIColor colorWithRed:0.2f  
  40.                                                       green:0.2f  
  41.                                                        blue:0.2f  
  42.                                                       alpha:0.75f];  
  43.         [contentView addSubview:textLabel];  
  44.         contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth;  
  45.         [contentView addTarget:self  
  46.                         action:@selector(toastTaped:)  
  47.               forControlEvents:UIControlEventTouchDown];  
  48.         contentView.alpha = 0.0f;  
  49.         [textLabel release];  
  50.         duration = DEFAULT_DISPLAY_DURATION;  
  51.         [[NSNotificationCenter defaultCenter] addObserver:self  
  52.                                                  selector:@selector(deviceOrientationDidChanged:)  
  53.                                                      name:UIDeviceOrientationDidChangeNotification  
  54.                                                    object:[UIDevice currentDevice]];  
  55.     }  
  56.     return self;  
  57. - (void)deviceOrientationDidChanged:(NSNotification *)notify_{  
  58.     [self hideAnimation];  
  59. -(void)dismissToast{  
  60.     [contentView removeFromSuperview];  
  61. -(void)toastTaped:(UIButton *)sender_{  
  62. - (void)setDuration:(CGFloat) duration_{  
  63.     duration = duration_;  
  64. -(void)showAnimation{  
  65.     [UIView beginAnimations:@"show" context:NULL];  
  66.     [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];  
  67.     [UIView setAnimationDuration:0.3];  
  68.     contentView.alpha = 1.0f;  
  69.     [UIView commitAnimations];  
  70. -(void)hideAnimation{  
  71.     [UIView beginAnimations:@"hide" context:NULL];  
  72.     [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];  
  73.     [UIView setAnimationDelegate:self];  
  74.     [UIView setAnimationDidStopSelector:@selector(dismissToast)];  
  75.     contentView.alpha = 0.0f;  
  76. - (void)show{  
  77.     UIWindow *window = [UIApplication sharedApplication].keyWindow;  
  78.     contentView.center = window.center;  
  79.     [window  addSubview:contentView];  
  80.     [self showAnimation];  
  81.     [self performSelector:@selector(hideAnimation) withObject:nil afterDelay:duration];  
  82. - (void)showFromTopOffset:(CGFloat) top_{  
  83.     contentView.center = CGPointMake(window.center.x, top_ + contentView.frame.size.height/2);  
  84. - (void)showFromBottomOffset:(CGFloat) bottom_{  
  85.     UIWindow *window = [UIApplication sharedApplication].keyWindow;      
  86.     contentView.center = CGPointMake(window.center.x, window.frame.size.height-(bottom_ + contentView.frame.size.height/2));  
  87. + (void)showWithText:(NSString *)text_{  
  88.     [OMGToast showWithText:text_ duration:DEFAULT_DISPLAY_DURATION];  
  89. + (void)showWithText:(NSString *)text_  
  90.             duration:(CGFloat)duration_{  
  91.     OMGToast *toast = [[[OMGToast alloc] initWithText:text_] autorelease];  
  92.     [toast setDuration:duration_];  
  93.     [toast show];  
  94.            topOffset:(CGFloat)topOffset_{  
  95.     [OMGToast showWithText:text_  topOffset:topOffset_ duration:DEFAULT_DISPLAY_DURATION];  
  96.            topOffset:(CGFloat)topOffset_  
  97.     [toast showFromTopOffset:topOffset_];  
  98.            bottomOffset:(CGFloat)bottomOffset_{  
  99.     [OMGToast showWithText:text_  bottomOffset:bottomOffset_ duration:DEFAULT_DISPLAY_DURATION];  
  100.         bottomOffset:(CGFloat)bottomOffset_  
  101.     [toast showFromBottomOffset:bottomOffset_];  
  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];