标签:弹出框
已经很长一段时间没有更新博客了,今天写了个弹出框,主要用于SDK里面
现贴出来分享下:
#import <Foundation/Foundation.h> @interface SYTipView : NSObject + (SYTipView *)shareInstance; - (void)showSuccessTipWithText:(NSString *)tipText; - (void)showErrorTipWithText:(NSString *)tipText; - (void)startWithText:(NSString *)tipText; - (void)dismissSuccessWithText:(NSString *)tipText block:(void(^)())action; - (void)dismissErrorWithText:(NSString *)tipText block:(void(^)())action; @end
#import "SYTipView.h" #define KEYWINDOW [UIApplication sharedApplication].keyWindow #define GetKeyWindow KEYWINDOW ? KEYWINDOW : [[UIApplication sharedApplication].windows objectAtIndex:0] #define TIPVIEW_WIDTH 100 #define TIPIMAGEVIEW_WIDTH 30 static SYTipView *syTipView = nil; @interface SYTipView ()<UITextViewDelegate> @property (nonatomic, strong) UIActivityIndicatorView *activityIndicatorView; @property (nonatomic, strong) UITextView *tipTextView; @property (nonatomic, strong) UIViewController *tipViewController; @property (nonatomic, strong) UIView *tipBgView; @property (nonatomic, strong) UIImageView *rightImageView; @property (nonatomic, strong) UIImageView *wrongImageView; @property (nonatomic, assign) BOOL isDisplaying; //限制不能重复点 @end @implementation SYTipView + (SYTipView *)shareInstance { static dispatch_once_t oneceToken; dispatch_once(&oneceToken, ^{ syTipView = [[[self class] alloc] init]; }); return syTipView; } - (id)init { self = [super init]; if (self) { UIWindow *keyWindow = GetKeyWindow; if (_tipViewController == nil) { _tipViewController = [[UIViewController alloc] init]; [_tipViewController.view setFrame:CGRectMake(0, 0, TIPVIEW_WIDTH, TIPVIEW_WIDTH)]; _tipViewController.view.center = CGPointMake(keyWindow.rootViewController.view.bounds.size.width * 0.5f, keyWindow.rootViewController.view.bounds.size.height * 0.5f); _tipViewController.view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin; } if (_activityIndicatorView == nil) { _activityIndicatorView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0 , 0, TIPVIEW_WIDTH, TIPVIEW_WIDTH)]; _activityIndicatorView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray; _activityIndicatorView.backgroundColor = [UIColor grayColor]; _activityIndicatorView.alpha = 0.5; _activityIndicatorView.layer.cornerRadius = 10; } if (_tipBgView == nil) { _tipBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, TIPVIEW_WIDTH, TIPVIEW_WIDTH)]; _tipBgView.layer.cornerRadius = 10; _tipBgView.backgroundColor = [UIColor grayColor]; _tipBgView.alpha = 0.5; } if (_rightImageView == nil) { NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"SYTipView" ofType:@"bundle"]; NSBundle *bundle = [NSBundle bundleWithPath:bundlePath]; NSString *imagePath = [bundle pathForResource:@"images/SY_arrow" ofType:@"png"]; _rightImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:imagePath]]; // NSString *const MJRefreshBundleName = @"SYTipView.bundle"; // NSString *path = [MJRefreshBundleName stringByAppendingPathComponent:@"images/SY_arrow.png"]; // UIImage *a = [UIImage imageNamed:path]; // _rightImageView = [[UIImageView alloc] initWithImage:a]; [_rightImageView setFrame:CGRectMake(0, 0, TIPIMAGEVIEW_WIDTH, TIPIMAGEVIEW_WIDTH)]; _rightImageView.center = CGPointMake(_tipViewController.view.bounds.size.width / 2, _tipViewController.view.bounds.size.height - 64); } if (_wrongImageView== nil) { NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"SYTipView" ofType:@"bundle"]; NSBundle *bundle = [NSBundle bundleWithPath:bundlePath]; NSString *imagePath = [bundle pathForResource:@"images/SY_wrong" ofType:@"png"]; _wrongImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:imagePath]]; [_wrongImageView setFrame:CGRectMake(0, 0, TIPIMAGEVIEW_WIDTH, TIPIMAGEVIEW_WIDTH)]; _wrongImageView.center = CGPointMake(_tipViewController.view.bounds.size.width / 2, _tipViewController.view.bounds.size.height - 64); } } return self; } #pragma mark - 构造TipTextView - (void)createTipTextView:(NSString *)tipText { //文字提示 _tipTextView = [[UITextView alloc] initWithFrame:CGRectMake(0 , 0, TIPVIEW_WIDTH, TIPVIEW_WIDTH)]; //让文字垂直居中 _tipTextView.delegate = self; [_tipTextView addObserver:self forKeyPath:@"contentSize" options:(NSKeyValueObservingOptionNew) context:nil]; _tipTextView.textAlignment = NSTextAlignmentCenter; _tipTextView.font = [UIFont boldSystemFontOfSize:14]; _tipTextView.backgroundColor = [UIColor clearColor]; _tipTextView.textColor = [UIColor whiteColor]; _tipTextView.userInteractionEnabled = NO; _tipTextView.text = tipText; } //#pragma mark - // //- (void)showTipWithText:(NSString *)tipText //{ // UIWindow *keyWindow = GetKeyWindow; // // _tipViewController.view.center = CGPointMake(keyWindow.rootViewController.view.bounds.size.width * 0.5f, keyWindow.rootViewController.view.bounds.size.height * 0.5f); // [_tipViewController.view addSubview:_activityIndicatorView]; // // [self createTipTextView:tipText]; // [_tipViewController.view addSubview:_tipTextView]; // // [keyWindow.rootViewController.view addSubview:_tipViewController.view]; //// [_activityIndicatorView startAnimating]; // // [self performSelector:@selector(dismissTipView) withObject:nil afterDelay:1]; //} //#pragma mark - 显示正确信息提示 // //- (void)dismissTipView //{ // [_activityIndicatorView stopAnimating]; // [_activityIndicatorView removeFromSuperview]; // [_tipTextView removeFromSuperview]; // [_tipViewController.view removeFromSuperview]; //} #pragma mark - 显示正确信息提示 - (void)showSuccessTipWithText:(NSString *)tipText { if (_isDisplaying) { return; } _isDisplaying = YES; UIWindow *keyWindow = GetKeyWindow; _tipViewController.view.center = CGPointMake(keyWindow.rootViewController.view.bounds.size.width * 0.5f, keyWindow.rootViewController.view.bounds.size.height * 0.5f); [_tipBgView addSubview:_rightImageView]; [self createTipTextView:tipText]; [_tipBgView addSubview:_tipTextView]; [_tipViewController.view addSubview:_tipBgView]; [keyWindow.rootViewController.view addSubview:_tipViewController.view]; [self performSelector:@selector(removeSuccessTipView) withObject:nil afterDelay:1]; } - (void)removeSuccessTipView { [_rightImageView removeFromSuperview]; [_tipTextView removeFromSuperview]; _tipTextView = nil; [_tipBgView removeFromSuperview]; [_tipViewController.view removeFromSuperview]; _isDisplaying = NO; } #pragma mark - 显示错误信息提示 - (void)showErrorTipWithText:(NSString *)tipText { if (_isDisplaying) { return; } _isDisplaying = YES; UIWindow *keyWindow = GetKeyWindow; _tipViewController.view.center = CGPointMake(keyWindow.rootViewController.view.bounds.size.width * 0.5f, keyWindow.rootViewController.view.bounds.size.height * 0.5f); [_tipBgView addSubview:_wrongImageView]; [self createTipTextView:tipText]; [_tipBgView addSubview:_tipTextView]; [_tipViewController.view addSubview:_tipBgView]; [keyWindow.rootViewController.view addSubview:_tipViewController.view]; [self performSelector:@selector(removeErrorTipView) withObject:nil afterDelay:1]; } - (void)removeErrorTipView { [_wrongImageView removeFromSuperview]; [_tipTextView removeFromSuperview]; _tipTextView = nil; [_tipBgView removeFromSuperview]; [_tipViewController.view removeFromSuperview]; _isDisplaying = NO; } #pragma mark - 开始提示 - (void)startWithText:(NSString *)tipText; { if (_isDisplaying) { return; } _isDisplaying = YES; UIWindow *keyWindow = GetKeyWindow; _tipViewController.view.center = CGPointMake(keyWindow.rootViewController.view.bounds.size.width * 0.5f, keyWindow.rootViewController.view.bounds.size.height * 0.5f); [_tipViewController.view addSubview:_activityIndicatorView]; [self createTipTextView:tipText]; [_tipViewController.view addSubview:_tipTextView]; [keyWindow.rootViewController.view addSubview:_tipViewController.view]; [_activityIndicatorView startAnimating]; } #pragma mark - 关闭有回调的成功提示 - (void)dismissSuccessWithText:(NSString *)tipText block:(void(^)())action { [_activityIndicatorView stopAnimating]; [_activityIndicatorView removeFromSuperview]; [_tipBgView addSubview:_rightImageView]; [_tipTextView removeFromSuperview]; _tipTextView = nil; [self createTipTextView:tipText]; [_tipBgView addSubview:_tipTextView]; [_tipViewController.view addSubview:_tipBgView]; [self performSelector:@selector(removeSuccessTipViewAction:) withObject:action afterDelay:1]; } - (void)removeSuccessTipViewAction:(void(^)())action { [_tipTextView removeFromSuperview]; _tipTextView = nil; [_rightImageView removeFromSuperview]; [_tipBgView removeFromSuperview]; [_tipViewController.view removeFromSuperview]; action(); _isDisplaying = NO; } #pragma mark - 关闭有回调的失败提示 - (void)dismissErrorWithText:(NSString *)tipText block:(void(^)())action { [_activityIndicatorView stopAnimating]; [_activityIndicatorView removeFromSuperview]; [_tipBgView addSubview:_wrongImageView]; [_tipTextView removeFromSuperview]; _tipTextView = nil; [self createTipTextView:tipText]; [_tipBgView addSubview:_tipTextView]; [_tipViewController.view addSubview:_tipBgView]; [self performSelector:@selector(removeErrorTipViewAction:) withObject:action afterDelay:1]; } - (void)removeErrorTipViewAction:(void(^)())action { [_tipTextView removeFromSuperview]; _tipTextView = nil; [_wrongImageView removeFromSuperview]; [_tipBgView removeFromSuperview]; [_tipViewController.view removeFromSuperview]; action(); _isDisplaying = NO; } //- (void)dismissTipViewAction:(void(^)())action //{ // [_activityIndicatorView stopAnimating]; // [_activityIndicatorView removeFromSuperview]; // [_tipTextView removeFromSuperview]; // [_tipViewController.view removeFromSuperview]; // action(); //} //- (void)dismissWithText:(NSString *)tipText block:(void(^)())action //{ // _tipTextView.text = tipText; // // [self performSelector:@selector(dismissTipViewAction:) withObject:action afterDelay:1]; //} #pragma mark - 文本垂直居中对齐或底部居中对齐KVC - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { UITextView *tempTextView = object; // //垂直居中对齐 // CGFloat topCorrect = (tempTextView.bounds.size.height - tempTextView.contentSize.height * tempTextView.zoomScale) / 2; // topCorrect = topCorrect < 0.0 ? 0.0 : topCorrect; // tempTextView.contentOffset = (CGPoint){.x = 0, .y = -topCorrect}; // Bottom vertical alignment CGFloat topCorrect = ([tempTextView bounds].size.height - [tempTextView contentSize].height); topCorrect = (topCorrect < 0.0 ? 0.0 : topCorrect); tempTextView.contentOffset = (CGPoint){.x = 0, .y = -topCorrect}; [tempTextView removeObserver:self forKeyPath:@"contentSize" context:nil]; } @end
标签:弹出框
原文地址:http://blog.csdn.net/liubo5290392/article/details/41750813