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

來源:互聯網
上載者:User

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

已經很長一段時間沒有更新部落格了,今天寫了個彈出框,主要用於SDK裡面

現貼出來分享下:

#import @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 30static SYTipView *syTipView = nil;@interface SYTipView ()@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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.