2010-06-01 5 views
4

Est-il possible de désactiver l'animation pour l'autorotation? Je veux qu'il tourne, mais je ne veux pas que l'animation se produise (comme un interrupteur instantané).iphone autorotation animation

+0

L'animation est la beauté de celui-ci! –

+0

Eh bien je l'ai mis en place pour que l'image BG change en une version horiz, ou vert de l'image en fonction de l'orientation et l'animation de rotation provoquait un pépin quand l'image a changé mais je l'ai joué d'accord quand même. – Brodie

Répondre

4

Il suffit d'ajouter le code suivant pour remplacer l'animation de rotation standard:

- (void)viewDidLoad 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil]; 
} 

- (void)didRotate:(NSNotification *)notification { 
    orientation = [[UIDevice currentDevice] orientation]; 
    if(orientation == UIDeviceOrientationUnknown || orientation == UIDeviceOrientationFaceDown || orientation == UIDeviceOrientationFaceUp){ 
     orientation = UIDeviceOrientationPortrait; 
    } 
    [[UIApplication sharedApplication] setStatusBarOrientation:orientation animated:NO]; 
    [self positionScreenElements]; 
} 
+1

Cette réponse a bien fonctionné, mais j'ai eu quelques problèmes lors de la mise au point d'un champ de texte. Si l'iPad était à plat sur le bureau, le clavier apparaissait à moitié hors écran - très étrange. Le problème était que l'orientation du périphérique pouvait également être inconnue, face vers le haut ou face vers le bas, sans que cela corresponde à l'orientation de la barre d'état. La vérification de ces états a rendu cela parfait. Un grand merci Nils pour m'avoir sauvé w/cette solution :) –

+0

+1 adam pour votre contribution:] –

+0

Qu'est ce que "orientation"? – Morkrom

7

Si vous avez vraiment besoin, il suffit d'utiliser setAnimationsEnabled de UIView:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    [UIView setAnimationsEnabled:NO]; // disable animations temporarily 
} 

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{ 
    [UIView setAnimationsEnabled:YES]; // rotation finished, re-enable them 
} 

Il fait l'affaire pour moi. Bien sûr, il n'est pas recommandé de le faire à moins d'avoir une très bonne raison de le faire.

+0

c'est super !, merci! –

0

@Nils Munch malheureusement cela ne fonctionne pas correctement, le setStatusBarOrientation attend UIInterfaceOrientation pas UIDeviceOrientation et le casting est une mauvaise pratique. Corrigez-moi si je me trompe