有一段時間沒更新部落格了。這幾天寫了一個提示框元件,主要用在 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 onceToken;
dispatch_once(&onceToken, ^{
syTipView = [[self 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;
}
// 從自帶的 resource bundle 載入圖示,避免與宿主 App 的資源命名衝突
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]];
[_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 - 建立文字區
- (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)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];
if (action) 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];
if (action) action();
_isDisplaying = NO;
}
#pragma mark - 讓文字靠下對齊
- (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;
topCorrect = (topCorrect < 0.0 ? 0.0 : topCorrect);
tempTextView.contentOffset = (CGPoint){.x = 0, .y = -topCorrect};
[tempTextView removeObserver:self forKeyPath:@"contentSize" context:nil];
}
@end
第一,用 isDisplaying 旗標避免連續點擊時疊出多層提示框,這是 SDK 元件經常被忽略的細節。
第二,圖示放在自己的 resource bundle 中載入,而不是直接用 imageNamed:。因為這個元件要嵌入別人的 App,若宿主專案剛好有同名的圖片資源就會取錯,用 bundle 隔離能避免衝突。
第三,UITextView 的文字預設是頂對齊,這裡透過 KVO 監聽 contentSize 變化,動態修正 contentOffset 讓文字靠下顯示,觀察完就立刻移除 observer,避免後患。
第四,Block 型別建議統一寫成 void (^)() 並在呼叫前判空,因為呼叫端有可能什麼都不傳。