iPhone自定义UITabBarController,好像是Instagram的作者写的。 自定义 tabbar

Full Source code: https://github.com/boctor/idev-recipes/tree/master/CustomTabBarNotification

Problem:

When the Instagram app wants to let you know that you have new comments, likes or followers, it doesn’t use standard badge values on tab bar items:

Instead it uses a custom tab bar notification view. How do we build a similar custom notification view?

Solution:

Just like in our recipe for the Twitter app’s current tab bar indicatorwe will add a view on top of the tab bar to notify the user. Instead of a static image, we will add a slightly more complicated view. This view has the background, icons for each of the activities (comments, likes, followers), and labels with the number value of each of these activities. Here is what it looks like laid out in the NIB:

Vertical/Horizontal Location

Again we need to figure out the horizontal and vertical location of our custom view and we essentially do the same thing we did in our Twitter tab bar indicator recipe:

To calculate the vertical location, we start at the top of the tab bar (0), go up by the height of the notification view, then go up another 2 pixels so our view is slightly above the tab bar

For the horizontal location we divide the width of the tab bar by the number of items to calculate the width of a single item. We then multiply the index by the width of single item and add half the width of an item to land in the middle.

Showing/Hiding the custom notification view

In a real app we would hit a web service, get the data and then notify the user by showing the custom notification UI.

In the sample app we have a couple of buttons that show and hide the notification view. We also do a very simple fade in/fade out animation of the notification view.

Showing the custom notification view:

view source
print ?
01 - (void) showNotificationViewFor:(NSUInteger)tabIndex
02 {
03   // To get the vertical location we start at the top of the tab bar (0), go up by the height of the notification view, then go up another 2 pixels so our view is slightly above the tab bar
04   CGFloat verticalLocation = - notificationView.frame.size.height - 2.0;
05   notificationView.frame = CGRectMake([selfhorizontalLocationFor:tabIndex], verticalLocation, notificationView.frame.size.width, notificationView.frame.size.height);
06  
07   if (!notificationView.superview)
08     [self.tabBar addSubview:notificationView];
09  
10   notificationView.alpha = 0.0;
11  
12   [UIView beginAnimations:nil context:nil];
13   [UIView setAnimationDuration:0.5];
14   notificationView.alpha = 1.0;
15   [UIView commitAnimations];
16 }

Hiding the custom notification view:

1 - (IBAction)hideNotificationView:(id)sender
2 {
3   [UIView beginAnimations:nil context:nil];
4   [UIView setAnimationDuration:0.5];
5   notificationView.alpha = 0.0;
6   [UIView commitAnimations];
7 }

In the end the solution is quite simple: A custom view laid out in a NIB, added to the view hierarchy at the right location. But it definitely looks sexier than the built in red badge.

What do you think? Can you make this code better? Let us know in the comments!

Full Source code: https://github.com/boctor/idev-recipes/tree/master/CustomTabBarNotification

你可能感兴趣的:(iPhone,animation,UIView,hierarchy,Comments,badge)