作用:
該懸浮窗顯示后切換viewController也會顯示轮蜕,需要手動或設(shè)置定時器使之消失恍飘。
用法:
在需要用到該懸浮提示窗的時候,使用工廠方法削解,并傳入?yún)?shù)2個參數(shù):AlertWindowPositionOption和要顯示的文字text营勤。
其中AlertWindowPositionOption為自定義的位置選項灵嫌,分別為:
AlertWindowPositionTopMiddle 靠窗口上部并縱向居中
AlertWindowPositionCenter 窗口中心位置
AlertWindowPositionBottomMiddle 靠窗口?下部并縱向居中
步驟:
step1:新建一個類CustomAlertWindow,基于UIWindow
step2:自定義該Window
分別在2個文件中寫入如下代碼:
//CustomAlertWindow.h
#import <UIKit/UIKit.h>
typedef NS_ENUM(NSInteger, AlertWindowPositionOption) {
AlertWindowPositionTopMiddle = 0,
AlertWindowPositionCenter,
AlertWindowPositionBottomMiddle
};
@interface CustomAlertWindow : UIWindow
+ (instancetype)aletWindowWithPositionOption:(AlertWindowPositionOption)positionType andInfoText:(NSString *)text;
//顯示
- (void)show;
// 消失
- (void)dismiss;
@end
//CustomAlertWindow.m
#import "CustomAlertWindow.h"
#define LABEL_MARGIN 10 //Label的外邊距
#define CENTER_Y_TO_EDGE 80 //懸浮窗中心點距邊緣的縱坐標(biāo)值
@implementation CustomAlertWindow
+ (instancetype)aletWindowWithPositionOption:(AlertWindowPositionOption)positionType andInfoText:(NSString *)text {
return [[CustomAlertWindow alloc] initWithPositionOption:positionType andInfoText:text];
}
- (instancetype)initWithPositionOption:(AlertWindowPositionOption)positionType andInfoText:(NSString *)text {
self = [super init];
if (self) {
UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 15)];
infoLabel.text = text;
infoLabel.textAlignment = NSTextAlignmentCenter;
infoLabel.font = [UIFont systemFontOfSize:13];
infoLabel.textColor = [UIColor whiteColor];
//label尺寸適應(yīng)文字大小
[infoLabel sizeToFit];
//根據(jù)labelSize確定window的大小
CGSize labelSize = infoLabel.frame.size;
CGSize windowSize = CGSizeMake(labelSize.width + 2*LABEL_MARGIN, labelSize.height + 2*LABEL_MARGIN);
//根據(jù)window的尺寸確定label的中心點位置
infoLabel.center = CGPointMake(windowSize.width / 2, windowSize.height / 2);
//得到屏幕的尺寸
CGRect superRect = [[UIScreen mainScreen] bounds];
CGSize superSize = superRect.size;
//計算window的大小
self.frame = CGRectMake((superSize.width - windowSize.width) / 2, 0, windowSize.width, windowSize.height);
//根據(jù)指定的positionType設(shè)置window的center
switch (positionType) {
case AlertWindowPositionTopMiddle:
self.center = CGPointMake(superSize.width / 2, CENTER_Y_TO_EDGE);
break;
case AlertWindowPositionCenter:
self.center = CGPointMake(superSize.width / 2, superSize.height / 2);
break;
case AlertWindowPositionBottomMiddle:
self.center = CGPointMake(superSize.width / 2, superSize.height - CENTER_Y_TO_EDGE);
break;
default:
break;
}
self.windowLevel = UIWindowLevelAlert + 1;
self.backgroundColor = [UIColor colorWithRed:64 / 255.0 green:64 / 255.0 blue:64 / 255.0 alpha:0.7];
self.layer.cornerRadius = 5;
[self addSubview:infoLabel];
}
return self;
}
//顯示window
- (void)show {
[self makeKeyAndVisible];
}
//window消失
- (void)dismiss {
[self resignKeyWindow];
}
@end
step3:調(diào)用的時候可以這樣處理:
#import "ViewController.h"
#import "CustomAlertWindow.h"
@interface ViewController ()
@property (nonatomic, strong) CustomAlertWindow *alertWindow;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)displayFloatingWindow:(id)sender {
self.alertWindow = [CustomAlertWindow aletWindowWithPositionOption:AlertWindowPositionCenter andInfoText:@"網(wǎng)絡(luò)不給力"];
[self.alertWindow show];
//設(shè)置定時器使之消失
[NSTimer scheduledTimerWithTimeInterval:4.0 target:self selector:@selector(resignWindow) userInfo:nil repeats:NO];
}
- (void)resignWindow {
[self.alertWindow dismiss];
//需要消失的時候不要忘了賦值nil
self.alertWindow = nil;
}
@end