Après plusieurs essais, voici comment je l'ai fait fonctionner pour ce que je voulais. Voilà ce que j'essayais. - J'ai une vue avec une image. et je voulais que l'image passe en plein écran. - J'ai aussi un contrôleur de navigation avec une tabBar. Je dois donc cacher ça aussi. - De plus, ma principale exigence n'était pas seulement de se cacher, mais aussi d'avoir un effet de décoloration lors de l'affichage et de la dissimulation.
C'est ainsi que je l'ai fait fonctionner.
Étape 1 - J'ai une image et l'utilisateur appuie une fois sur cette image. Je capture ce geste et le pousse dans le nouveau imageViewController, c'est dans le imageViewController, je veux avoir une image plein écran.
- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer {
NSLog(@"Single tap");
ImageViewController *imageViewController =
[[ImageViewController alloc] initWithNibName:@"ImageViewController" bundle:nil];
godImageViewController.imgName = // pass the image.
godImageViewController.hidesBottomBarWhenPushed=YES;// This is important to note.
[self.navigationController pushViewController:godImageViewController animated:YES];
// If I remove the line below, then I get this error. [CALayer retain]: message sent to deallocated instance .
// [godImageViewController release];
}
Étape 2 - Toutes ces étapes ci-dessous sont dans ImageViewController
Étape 2.1 - Dans ViewDidLoad, affichez la barre de navigation
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSLog(@"viewDidLoad");
[[self navigationController] setNavigationBarHidden:NO animated:YES];
}
Étape 2.2 - Dans viewDidAppear, configurez une tâche de minuterie avec retard (je l'ai définie pour un délai de 1 seconde). Et après le délai, ajoutez un effet de décoloration. J'utilise alpha pour utiliser la décoloration.
- (void)viewDidAppear:(BOOL)animated
{
NSLog(@"viewDidAppear");
myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(fadeScreen) userInfo:nil repeats:NO];
}
- (void)fadeScreen
{
[UIView beginAnimations:nil context:nil]; // begins animation block
[UIView setAnimationDuration:1.95]; // sets animation duration
self.navigationController.navigationBar.alpha = 0.0; // Fades the alpha channel of this view to "0.0" over the animationDuration of "0.75" seconds
[UIView commitAnimations]; // commits the animation block. This Block is done.
}
étape 2.3 - Sous viewWillAppear, ajoutez un geste singleTap à l'image et rendez la barre de navigation translucide.
- (void) viewWillAppear:(BOOL)animated
{
NSLog(@"viewWillAppear");
NSString *path = [[NSBundle mainBundle] pathForResource:self.imgName ofType:@"png"];
UIImage *theImage = [UIImage imageWithContentsOfFile:path];
self.imgView.image = theImage;
// add tap gestures
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[self.imgView addGestureRecognizer:singleTap];
[singleTap release];
// to make the image go full screen
self.navigationController.navigationBar.translucent=YES;
}
- (void)handleTap:(UIGestureRecognizer *)gestureRecognizer
{
NSLog(@"Handle Single tap");
[self finishedFading];
// fade again. You can choose to skip this can add a bool, if you want to fade again when user taps again.
myTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(fadeScreen) userInfo:nil repeats:NO];
}
Étape 3 - Enfin viewWillDisappear, assurez-vous de remettre tous les trucs
- (void)viewWillDisappear: (BOOL)animated
{
self.hidesBottomBarWhenPushed = NO;
self.navigationController.navigationBar.translucent=NO;
if (self.navigationController.topViewController != self)
{
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
[super viewWillDisappear:animated];
}