在状态栏上做渐变动画效果
效果
说明
1. 在状态栏上显示信息并覆盖状态栏信息需要技巧
2. 本设计支持外部消息view的定制,只需要实现规定的协议方法即可
源码
https://github.com/YouXianMing/StatusBarMessage
//
// StatusMessage.h
// StatusBarView
//
// Created by YouXianMing on 15/7/24.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "StatusMessageProtocol.h"
@interface StatusMessage : UIView
/**
* 显示加载的view
*
* @param view 实现了StatusMessageProtocol的view
* @param duration 动画显示时间
*/
+ (void)showWithView:(UIView <StatusMessageProtocol> *)view duration:(NSTimeInterval)duration;
/**
* 隐藏加载的view
*
* @param duration 隐藏动画显示时间
*/
+ (void)hideWithDuration:(NSTimeInterval)duration;
///////////////////////////////////////////////////////////////////////////////////////////////////
/**
* 设置显示以及隐藏的动画时间(不设置,默认为1s)
*
* @param duration 动画时间
*/
+ (void)showAndHideDuration:(NSTimeInterval)duration;
/**
* 显示view,然后几秒后隐藏
*
* @param view 实现了StatusMessageProtocol的view
* @param seconds 延时几秒后隐藏
*/
+ (void)showWithView:(UIView<StatusMessageProtocol> *)view hideAfterSeconds:(NSTimeInterval)seconds;
@end
//
// StatusMessage.m
// StatusBarView
//
// Created by YouXianMing on 15/7/24.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
//
#import "StatusMessage.h"
#define Status_Message @"StatusMessage"
static UIWindow *_statusWindow = nil;
static NSMapTable *_weakDictionary = nil;
static NSTimeInterval _animationDuration = 1.f;
@implementation StatusMessage
+ (void)initialize {
if (self == [StatusMessage class]) {
_weakDictionary = [NSMapTable strongToWeakObjectsMapTable];
_statusWindow = [[UIWindow alloc] initWithFrame:STATUS_BAR_FRAME];
_statusWindow.windowLevel = UIWindowLevelStatusBar + 1;
_statusWindow.userInteractionEnabled = NO;
[_statusWindow makeKeyAndVisible];
}
}
+ (void)showWithView:(UIView <StatusMessageProtocol> *)view duration:(NSTimeInterval)duration {
[_weakDictionary setObject:view forKey:Status_Message];
[_statusWindow addSubview:view];
[view showWithDuration:duration];
}
+ (void)hideWithDuration:(NSTimeInterval)duration {
UIView <StatusMessageProtocol> *tmpView = [_weakDictionary objectForKey:Status_Message];
[tmpView hideWithDuration:duration];
}
+ (void)showAndHideDuration:(NSTimeInterval)duration {
_animationDuration = duration;
}
+ (void)showWithView:(UIView<StatusMessageProtocol> *)view hideAfterSeconds:(NSTimeInterval)seconds {
[_weakDictionary setObject:view forKey:Status_Message];
[_statusWindow addSubview:view];
[view showWithDuration:_animationDuration];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, seconds * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
UIView <StatusMessageProtocol> *tmpView = [_weakDictionary objectForKey:Status_Message];
[tmpView hideWithDuration:_animationDuration];
});
}
@end
//
// StatusMessageProtocol.h
// StatusBarView
//
// Created by YouXianMing on 15/7/24.
// Copyright (c) 2015年 YouXianMing. All rights reserved.
//
#import <Foundation/Foundation.h>
/**
* 状态栏的frame值
*/
#define STATUS_BAR_FRAME [[UIApplication sharedApplication] statusBarFrame]
@protocol StatusMessageProtocol <NSObject>
@required
/**
* 显示动画
*
* @param seconds 显示持续时间
*/
- (void)showWithDuration:(NSTimeInterval)seconds;
/**
* 隐藏动画(隐藏动画的时候请移除掉自己)
*
* @param seconds 隐藏持续时间
*/
- (void)hideWithDuration:(NSTimeInterval)seconds;
@end