Twitter-like Notification
I really like how inapp notifications work in the latest version of Twitter for iPhone (July 2012). This approach also seems to be very easy to implement so I decided to create a very easy example showing one way how this can be done.
The trick is to add a label or whatever you want to application’s window. That’s an easy task since UIWindow is a sublass of UIView. Once you have this done, you can easily hide the native statusbar and display the label.
//
// NotifWindow.m
// TwitterLikeNotification
//
// Created by Petr Pavlik on 7/11/12.
// Copyright (c) 2012 Petr Pavlik. All rights reserved.
//
#import "NotifWindow.h"
@interface NotifWindow ()
@property(nonatomic, strong) UILabel* notificationLabel;
@end
@implementation NotifWindow
@synthesize notificationLabel = _notificationLabel;
- (void) showNotificationWithText:(NSString*)text {
if (!self.notificationLabel) {
self.notificationLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
self.notificationLabel.textColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.8];
self.notificationLabel.backgroundColor = [UIColor blackColor];
self.notificationLabel.textAlignment = UITextAlignmentCenter;
self.notificationLabel.font = [UIFont boldSystemFontOfSize:13.0f];
[self addSubview:self.notificationLabel];
}
self.notificationLabel.text = text;
self.notificationLabel.frame = CGRectMake(0, -20, 320, 20);
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
__weak NotifWindow* weakSelf = self;
double delayInSeconds = 0.5;
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:^{
weakSelf.notificationLabel.frame = CGRectMake(0, 0, 320, 20);
}];
});
delayInSeconds = 4.0;
popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[UIView animateWithDuration:0.5 animations:^{
weakSelf.notificationLabel.frame = CGRectMake(0, -20, 320, 20);
}];
});
delayInSeconds = 4.5;
popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
});
}
@end
And this is how it can be used.
- (IBAction)showNotification:(id)sender {
NotifWindow* notifWindow = (NotifWindow*)self.view.window;
[notifWindow showNotificationWithText:@"Nice!"];
}
Please note that this is a very simple example. If you want to implement such functionality into your app, you shout take care of things like handling of landscape mode or queuing of notifications.
Hello world!
Welcome to WordPress.com. After you read this, you should delete and write your own post, with a new title above. Or hit Add New on the left (of the admin dashboard) to start a fresh post.
Here are some suggestions for your first post.
- You can find new ideas for what to blog about by reading the Daily Post.
- Add PressThis to your browser. It creates a new blog post for you about any interesting page you read on the web.
- Make some changes to this page, and then hit preview on the right. You can always preview any post or edit it before you share it to the world.