iOS自定义控件:提醒条

iOS自定义控件:提醒条_第1张图片
镇楼专用

简单的提醒条,代码比较简单,使用容易,代码中目前提供了三种显示的颜色,默认显示文本内容为一行文字,默认高度为20,可以根据自己需求自定义样式。代码同时支持Objective-C和Swift,使用方式有点点差别,详情看代码。

接口文件

#import 

NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(NSInteger, RemindBarStyle) {
    RemindBarStyleInfo, // 提醒 ❇️
    RemindBarStyleWarn, // 警告 ⚠️
    RemindBarStyleError // 错误 ❌
};

typedef NS_ENUM(NSInteger, RemindBarPosition) {
    RemindBarPositionStatusBar,     // 状态栏下面
    RemindBarPositionNavigationBar, // 导航栏下面
};

@interface LZRemindBar : UIView

/**
 配置显示样式

 @param style 样式
 @param position 位置
 @param text 内容
 @return return value description
 */
+ (LZRemindBar *)configurationWithStyle:(RemindBarStyle)style showPosition:(RemindBarPosition)position contentText:(NSString *)text;

/**
 显示 并设置显示时间

 @param interval 显示时间
 */
- (void)showBarAfterTimeInterval:(NSTimeInterval)interval;

@end
NS_ASSUME_NONNULL_END

实现文件

#import "LZRemindBar.h"

#define bar_IsPhoneX  UIApplication.sharedApplication.statusBarFrame.size.height != 20
#define bar_topInterval  bar_IsPhoneX ? UIApplication.sharedApplication.statusBarFrame.size.height : 0

@interface LZRemindBar ()

@property (nonatomic, strong) UILabel           *contentLabel; //*! 文字Label
@property (nonatomic, assign) RemindBarPosition position;      //*! 显示位置
@property (nonatomic, assign) BOOL              isShowing;     //*! 是否正在显示
@end

@implementation LZRemindBar

+ (instancetype)remind {
    static dispatch_once_t onceToken;
    static LZRemindBar *remind;
    dispatch_once(&onceToken, ^{
        remind = [[LZRemindBar alloc] init];
    });
    return remind;
}

+ (LZRemindBar *)configurationWithStyle:(RemindBarStyle)style showPosition:(RemindBarPosition)position contentText:(NSString *)text {
    
    if ([LZRemindBar remind].isShowing) {
        return [LZRemindBar remind];
    }
    
    [LZRemindBar remind].contentLabel = ({
        UILabel *label = [[UILabel alloc] init];
        label.backgroundColor = UIColor.clearColor;
        label.numberOfLines = 1;
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:13];
        label.textColor = UIColor.whiteColor;
        [[LZRemindBar remind] addSubview:label];
        label.translatesAutoresizingMaskIntoConstraints = false;
        [[label.leadingAnchor constraintEqualToAnchor:[LZRemindBar remind].leadingAnchor] setActive:true];
        [[label.trailingAnchor constraintEqualToAnchor:[LZRemindBar remind].trailingAnchor] setActive:true];
        [[label.topAnchor constraintEqualToAnchor:[LZRemindBar remind].topAnchor] setActive:true];
        [[label.bottomAnchor constraintEqualToAnchor:[LZRemindBar remind].bottomAnchor] setActive:true];
        label.text = text;
        label;
    });
    
    [LZRemindBar remind].isShowing = true;
    [LZRemindBar remind].position  = position;
    
    switch (style) {
        case RemindBarStyleInfo:
            [LZRemindBar remind].backgroundColor = [UIColor colorWithRed:0 green:166.0/255 blue:124.0/255 alpha:1];
            break;
        case RemindBarStyleWarn:
            [LZRemindBar remind].backgroundColor = UIColor.orangeColor;
            break;
        case RemindBarStyleError:
            [LZRemindBar remind].backgroundColor = UIColor.redColor;
            break;
        default:
            break;
    }
    
    if (position == RemindBarPositionStatusBar) {
        [[LZRemindBar remind] setupShowStatusBarBottomWithStutus:true];
    } else {
        [[LZRemindBar remind] setupShowNavigationBarBottomWithStutus:true];
    }
    
    return [LZRemindBar remind];
}

- (void)setupShowStatusBarBottomWithStutus:(BOOL)show {
    if (show) {
        UIApplication.sharedApplication.delegate.window.windowLevel = bar_IsPhoneX ? UIWindowLevelNormal : UIWindowLevelAlert;
        self.frame = CGRectMake(0, bar_topInterval, UIScreen.mainScreen.bounds.size.width, 0);
        [UIView animateWithDuration:0.5 animations:^{
            self.frame = CGRectMake(0, bar_topInterval, UIScreen.mainScreen.bounds.size.width, 20);
        }];
    } else {
        [UIView animateWithDuration:0.5 animations:^{
            self.frame = CGRectMake(0, bar_topInterval, UIScreen.mainScreen.bounds.size.width, 0);
        } completion:^(BOOL finished) {
            UIApplication.sharedApplication.delegate.window.windowLevel = UIWindowLevelNormal;
            [self dismissBar];
        }];
    }
}

- (void)setupShowNavigationBarBottomWithStutus:(BOOL)show {
    if (show) {
        self.frame = CGRectMake(0, UIApplication.sharedApplication.statusBarFrame.size.height + 44, UIScreen.mainScreen.bounds.size.width, 0);
        [UIView animateWithDuration:0.5 animations:^{
            self.frame = CGRectMake(0, UIApplication.sharedApplication.statusBarFrame.size.height + 44, UIScreen.mainScreen.bounds.size.width, 20);
        }];
    } else {
        [UIView animateWithDuration:0.5 animations:^{
            self.frame = CGRectMake(0, UIApplication.sharedApplication.statusBarFrame.size.height + 44, UIScreen.mainScreen.bounds.size.width, 0);
        } completion:^(BOOL finished) {
            [self dismissBar];
        }];
    }
}

- (void)showBarAfterTimeInterval:(NSTimeInterval)interval {
    [UIApplication.sharedApplication.delegate.window addSubview:self];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(interval * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (self.position == RemindBarPositionStatusBar) {
            [self setupShowStatusBarBottomWithStutus:false];
        } else {
            [self setupShowNavigationBarBottomWithStutus:false];
        }
    });
}

- (void)dismissBar {
    self.isShowing = false;
    [self.contentLabel removeFromSuperview];
    [self removeFromSuperview];
}
@end

使用方法

//  参数RemindBarStyle: 显示样式
//  参数RemindBarPosition: 显示位置 
//  参数contentText: 显示文字内容 
//  interval:显示时间

// Objc
[[LZRemindBar configurationWithStyle:RemindBarStyleInfo showPosition:RemindBarPositionStatusBar contentText:@"这是一个提示"] showBarAfterTimeInterval:1.2];
// Swift
LZRemindBar.configuration(with: .info, show: .statusBar, contentText: "提示文字").show(afterTimeInterval: 1.2)

效果图

iOS自定义控件:提醒条_第2张图片

你可能感兴趣的:(iOS自定义控件:提醒条)