覆盖stateBar的通知效果
先看效果图:
这只是最简单实现该效果的方式,如需更为出色的效果请自行百度。
下面是代码:
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *showNotificationButton = [UIButton buttonWithType:UIButtonTypeCustom];
showNotificationButton.frame = CGRectMake(0, 0, 150, 50);
showNotificationButton.center = self.view.center;
showNotificationButton.backgroundColor = [UIColor blueColor];
[showNotificationButton setTitle:@"show notification" forState:UIControlStateNormal];
[showNotificationButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:showNotificationButton];
}
- (void)clickButton:(id)sender {
CGRect frame = [UIApplication sharedApplication].statusBarFrame;
UILabel *alertLabel = [[UILabel alloc] init];
frame.origin.y -= 20;
alertLabel.text = @"new notification !";
alertLabel.frame = frame;
alertLabel.textColor = [UIColor whiteColor];
alertLabel.font = [UIFont systemFontOfSize:11];
alertLabel.alpha = 1.0;
alertLabel.textAlignment = NSTextAlignmentCenter;
alertLabel.backgroundColor = [UIColor brownColor];
UIView *view = [UIApplication sharedApplication].keyWindow.rootViewController.view;
[view addSubview:alertLabel];
frame.origin.y += 20;
UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
rootWindow.windowLevel = UIWindowLevelAlert;
[UIView animateWithDuration:0.25 animations:^{
alertLabel.frame = frame;
} completion:^(BOOL finished) {
[NSThread sleepForTimeInterval:2];
CGRect frame = [UIApplication sharedApplication].statusBarFrame;
frame.origin.y -= 20;
[UIView animateWithDuration:0.25 animations:^{
alertLabel.frame = frame;
dispatch_async(dispatch_get_main_queue(), ^{
[NSThread sleepForTimeInterval:0.25];
UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
rootWindow.windowLevel = UIWindowLevelNormal;
[alertLabel removeFromSuperview];
});
}];
}];
}