How To: Animate Showing and Hiding a UINavigationBar with Tap Gesture

The navigation bar is an important component of your app’s user interface but, sometimes, it can really get in the way of your content. Maybe you’d like to show a web page or an image, but the navigation bar occupies precious screen real estate, which essentially becomes dead space while a user is engaged with your content.

This tutorial quickly teaches you how to animate showing and hiding the navigation bar with a tap gesture.

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>;

#define kNavBarDefaultPosition CGPointMake(160,22)

@interface UIViewController () <UIGestureRecognizerDelegate>;

@end

@implementation UIViewController

- (void)viewDidLoad:(BOOL)animated
{
    [super viewDidLoad];

    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer]
    alloc] initWithTarget:self action:@slector(animateNavigationBar)];
    recognizer.delegate = self;
    recognizer.numberOfTapsRequired = 1;
    [self.view addGestureRecognizer:recognizer];
}

- (void)animateNavigationBar
{
    CALayer = self.navigationController.navigationBar.layer;

    // If the navigation bar is shown, hide it.
    // Else, if the navigation bar is hidden, show it.

    if (layer.position.x == kNavBarDefaultPosition.x &amp;&amp;
    layer.position.y == kNavBarDefaultPosition.y) {
         [UIView animateWithDuration:0.25 animations:^{
             layer.position = CGPointMake(layer.position.x,
             -self.navigationController.navigationBar.frame.
             size.height);
         }];
    } else {
        [UIView animateWithDuration:0.25 animations:^{
            layer.position = kNavBarDefaultPosition;
        }];
    }
}
Advertisements

How To: Programatically Select a Random Cell in a UITableView

This little snippet of code shows how you can select a random UITableViewCell in a UITableView programatically. For example, you might want to trigger a random selection when the user shakes the device, or through some other action of your determination. Here I assume your familiarity with setting up a UITableView and implementing its associated delegate and datasource methods.

@interface MyTableViewController : UITableViewController {

// Declare arrays in @interface

    NSArray *section1;
    NSArray *section2;
    NSArray *section3;

    NSArray *randomSectionsArray;
}

- (void)viewDidLoad {

// Initialize arrays
    section1 = @[string1, string2, string3];
    section2 = @[string4, string5, string6]:
    section3 = @[string7, string8, string9];

    sectionsArray  = @[section1, section2,
    section3];
}

/* After implementing the delegate and datasource methods for the UITableView, 
   you can now implement the method to select a UITableViewCell at a random indexPath: */

- (void)selectCellAtRandomIndexPath
{
    int randomSectionIndex = arc4random() % sectionsArray.count;

    NSArray *randomSection = sectionsArray[randomSectionIndex];
    int randomRow = arc4random() % randomSection.count;

    NSIndexPath *randomIndexPath = [NSIndexPath 
    indexPathForRow:randomRow inSection:randomSection];

    [self.tableView selectRowAtIndexPath animated:NO
    scrollPosition:UITableViewScrollPositionMiddle];

    [self.tableView.delegate tableView:self.tableView
    didSelectRowAtIndexPath:[self.tableView indexPathForSelectedRow];

}


- (void)triggerAction
{
   //  Now, you can use your trigger action to select a random cell.

    [self selectCellAtRandomIndexPath];
}

That’s it! Happy coding.