多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > 綜合技術 > IOS7風格彈出框-支持block回調

IOS7風格彈出框-支持block回調

來源:程序員人生   發布時間:2014-12-16 08:57:57 閱讀次數:3123次

已很長1段時間沒有更新博客了,今天寫了個彈出框,主要用于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

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 亚洲日韩精品欧美一区二区一 | 欧美在线一级va免费观看 | 最近中文在线国语 | 波多野吉衣 免费一区 | 激情影院网站 | 免费观看成年欧美1314www色 | 日本啊啊啊 | 激情小说图片 | 香蕉成人啪国产精品视频综合网 | 亚洲高清免费在线观看 | 波多野结衣久久高清免费 | 一级做a爰片性色毛片黄书 一级做a爰片性色毛片男 | 播放四川美女一级毛片半小时 | 国产一级精品绿帽视频 | 国产亚洲综合成人91精品 | 免费jizz在线播放视频高清版 | 在线观看视频在线观看 | 国产美女一区精品福利视频 | 综合亚洲欧美 | 99热久久久这里只有精品免费 | 猫咪www免费人成网站 | 亚洲一区二区三区四区在线观看 | 网友自拍区一区二区三区 | 野外性xxxxfrxxxx | 日韩在线一区高清在线 | 日韩欧美亚洲 | 欧美成人影院免费观 | 毛片专区| 国产91久久精品一区二区 | 亚洲欧美日韩在线 | 国产一级做性视频 | 免费观看无遮挡www的视频 | 国产精品欧美亚洲区 | 日韩欧美小说 | 日韩欧美亚州 | 涩涩免费播放观看在线视频 | 亚洲一区二区综合 | 中国美女隐私无遮挡免费视频 | 日韩一级欧美一级毛片在 | 亚洲欧美在线视频免费 | 亚洲天堂影视 |