Après de multiples essais, voici comment j'ai réussi à obtenir ce que je voulais. Voici ce que j'essayais. - J'ai une vue avec une image, et je voulais que l'image passe en plein écran. - J'ai un contrôleur de navigation avec une barre d'onglets également. J'ai donc besoin de la cacher aussi. - En outre, ma principale exigence n'était pas seulement de cacher, mais d'avoir un effet de fondu pendant l'affichage et le masquage.
Voici comment je l'ai fait fonctionner.
Étape 1 - J'ai une image et l'utilisateur tape une fois sur cette image. Je capture ce geste et le pousse dans le nouveau fichier de l'image. imageViewController
il est dans le imageViewController
Je veux avoir une image en 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 les étapes ci-dessous sont dans le ImageViewController
Étape 2.1 - Dans ViewDidLoad, afficher la navBar
- (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
J'ai mis en place une tâche de minuterie avec un délai (j'ai mis un délai de 1 seconde). Et après le délai, ajoutez un effet de fondu. J'utilise l'alpha pour utiliser le fondu.
- (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 le 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 dans viewWillDisappear
assure-toi de remettre toutes les choses en place
- (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];
}