类似微博发送信息时,在状态栏提示消息的效果。主要实现思想是继承UIWindow,然后设置windowlevel的级别高于UIStatusBar。最后把自定义的statusbar放置在原来的statusbar指定的位置上。
#import <UIKit/UIKit.h>
@interface SendVoiceTipsView : UIWindow
{
UILabel* messageLabel;
}
-(void)startTips:(NSString*)startMsg;
-(void)stopTips:(NSString*)stopMsg;
@end
#import "SendVoiceTipsView.h"
#import <QuartzCore/QuartzCore.h>
@implementation SendVoiceTipsView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.windowLevel = UIWindowLevelAlert + 1;
self.backgroundColor = [UIColor clearColor];
messageLabel = [[UILabel alloc]initWithFrame:CGRectMake(320*2/3, 0, 320/3, frame.size.height)];
messageLabel.backgroundColor = [UIColor blackColor];
messageLabel.textColor = [UIColor grayColor];
messageLabel.textAlignment = UITextAlignmentRight;
messageLabel.font = [UIFont systemFontOfSize:13];
[self addSubview:messageLabel];
}
return self;
}
//显示在状态栏上提示发送说说中
-(void)startTips:(NSString *)startMsg{
messageLabel.text = startMsg;
self.windowLevel = UIWindowLevelAlert + 1;
[self makeKeyAndVisible];
}
//隐藏状态栏上的提示
-(void)stopTips:(NSString *)stopMsg{
messageLabel.layer.transform = CATransform3DMakeRotation(M_PI * 0.5, 1, 0, 0);
[UIView animateWithDuration:0.7 animations:^{
messageLabel.layer.transform = CATransform3DIdentity;
messageLabel.text = stopMsg;
}completion:^(BOOL finished){
double delayInSeconds = 1;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[UIView animateWithDuration:0.5 animations:^{
messageLabel.layer.transform = CATransform3DMakeRotation(M_PI * 0.5, -1, 0, 0);
}completion:^(BOOL finished){
self.windowLevel = UIWindowLevelStatusBar - 1;
[[[UIApplication sharedApplication].delegate window] makeKeyAndVisible];
}];
});
}];
}
@end