iOS懸浮提示窗

作用:

該懸浮窗顯示后切換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       //懸浮窗中心點距邊緣的縱坐標值

@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尺寸適應文字大小
        [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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容