Essayer de comprendre comment devrait-ilautorotaToInterfaceOrientation et UideviceOrientationDidChangenotification

StackOverflow https://stackoverflow.com/questions/9514047

Question

J'ai une interface que je veux démarrer dans l'orientation du paysage. Après le démarrage, lorsque l'utilisateur fait pivoter l'appareil au portrait, j'affiche un calendrier de vue d'une journée. Lors du retour à l'orientation du paysage, le calendrier est rejeté. Tout fonctionne très bien dans chaque orientation avec mon interface utilisateur d'application affichant correctement dans l'orientation du paysage et le calendrier affichant correctement dans l'orientation du portrait.

Le problème est de savoir si l'utilisateur tient l'iPhone dans l'orientation du paysage au démarrage. Peu importe ce que je fais, je ne peux pas le faire démarrer avec mon interface utilisateur en mode paysage. Ma méthode UideviceOrientationDidChangenotification se déclenche deux fois, la première fois [Uidevice CurrentDevice]. L'orientation est le paysage, le second est le portrait. Le résultat final est que l'interface utilisateur tourne en mode portrait et affiche la vue du jour. Pas ce que je veux. Je veux que l'interface utilisateur reste dans l'orientation du paysage jusqu'à ce que l'utilisateur pivote physiquement l'appareil du paysage au portrait.

Je ne comprends pas pourquoi il tire avec un paysage [Uidevice CurrentDevice]. ORIENTATION Lorsque l'utilisateur tient l'appareil dans l'orientation du portrait.

Voici à quoi ressemble mon code dans le ViewController ...

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {       

    if ((interfaceOrientation == UIDeviceOrientationPortrait)|| (interfaceOrientation == UIDeviceOrientationPortraitUpsideDown)) {            
        return NO;
    } 

    return YES;
}


- (void)viewDidLoad {
    [super viewDidLoad];
        showingCalendar = NO;
        initializing=YES;
        [[UIDevice currentDevice]    beginGeneratingDeviceOrientationNotifications];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(didRotate:)
                                             name:UIDeviceOrientationDidChangeNotification
                                           object:nil];

}

-(void)didRotate:(NSNotification *)notification {
    UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;  
    if ((deviceOrientation == UIDeviceOrientationPortrait) || (deviceOrientation == UIDeviceOrientationPortraitUpsideDown))  {            
        if ((!showingCalendar) && (!initializing)) {
            showingCalendar = YES;
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];            
            GCCalendarPortraitView *calendar = [[[GCCalendarPortraitView alloc] init] autorelease];
            navigationController = [[UINavigationController alloc] initWithRootViewController:calendar];
            [self presentModalViewController:navigationController animated:YES];
        }
    }else if ((deviceOrientation == UIDeviceOrientationLandscapeRight) || (deviceOrientation == UIDeviceOrientationLandscapeLeft)) {
        if (showingCalendar) {
            showingCalendar = NO;
            if (deviceOrientation == UIDeviceOrientationLandscapeRight){
                [self dismissModalViewControllerAnimated:YES];
            }else if (deviceOrientation == UIDeviceOrientationLandscapeLeft){
                [self dismissModalViewControllerAnimated:YES];
            }
        }else {
            initializing = NO;  
        }            
    }
}
Était-ce utile?

La solution

J'ai trouvé une solution de contournement de mon problème. Dans ViewDidload, j'ai lancé un ScheduledTimemerWithTimeInterval et j'ai déplacé BegingEneratingDeviceORientationNotifications vers la méthode du sélecteur.

Maintenant, la notification ne tire jamais plus d'une fois. L'utilisateur obtient un paysage au démarrage, peu importe dans quelle direction l'appareil est maintenu et après le démarrage, toutes les rotations fonctionnent parfaitement.

Voici mon code modifié. Tout le reste est resté le même ...

- (void)viewDidLoad {
    [super viewDidLoad];
    showingCalendar = NO;
    initializing=YES;
    timer =  [NSTimer scheduledTimerWithTimeInterval:.55 target:self selector:@selector(startOrientationNotifications) userInfo:nil repeats: NO];

}


-(void)startOrientationNotifications {
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(didRotate:)
                                             name:UIDeviceOrientationDidChangeNotification
                                           object:nil];

}

Autres conseils

Je ne générerais pas de NOTIFICATION DES DÉVICEMENTS ORDES

Un moyen simple pourrait être d'utiliser un bool pour vérifier quand le portrait est autorisé dans l'autautorotaToInterfaceOrientation

quelque chose comme ça:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {       

    if ((interfaceOrientation == UIDeviceOrientationPortrait)|| (interfaceOrientation == UIDeviceOrientationPortraitUpsideDown)) {   

        return portraitIsAllowed;
    } 

    return YES;
}

Ensuite, changez-le en cas de besoin dans d'autres méthodes.

Et gardez à l'esprit que les devus AautorotateToInterfaceOrientation sont appelés chaque fois que l'utilisateur tourne le périphérique et aussi lorsque vous chargez (instancier) votre contrôleur la première fois

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top