comment traiter UIDeviceOrientationFaceUp et UIDeviceOrientationFaceDown?
-
09-10-2019 - |
Question
une question noob ici.
i détecter l'orientation avec:
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
tout est bien beau et je me repositionner les champs de texte et les étiquettes selon l'orientation d'un rapport avec
if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown)
et else {} pour tout le reste
le problème que je récemment découvert est lorsque le UIDevice
rapporte UIDeviceOrientationFaceUp
ou UIDeviceOrientationFaceDown
. comment puis-je faire face à cette situation? comment puis-je savoir si UIDeviceOrientationFaceUp
et UIDeviceOrientationFaceDown
se passe dans Portrait ou Paysage? Je sais que l'appareil est orienté vers le haut ou vers le bas, mais je ne sais pas si je dois repositionner tout en mode portrait ou paysage.
merci!
La solution
Apple recommande de ne pas utiliser l'appareil orientation pour la mise en page de la vue. Au lieu de cela, chaque contrôleur de vue a une propriété de interfaceOrientation et UIApplication
a propriété de statusBarOrientation, les deux qui retournera le courant Interface orientation, ce qui est adapté pour la mise vue.
Pour surveiller les changements, il existe des méthodes de UIViewController
comme willRotateToInterfaceOrientation: durée: qui sera appelée et des notifications telles que UIApplicationWillChangeStatusBarOrientationNotification qui sera affiché lors d'un changement d'orientation de l'interface se produit .
Autres conseils
Un peu en retard, il nous l'espérons aider quelqu'un.
Pour iOS 6.0 et versions ultérieures:
1) Définir ce code lors de votre configuration Affichage pour recevoir une notification de rotation:
// Set Listener to receive orientation notifications from the device
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(detectOrientation)
name:UIDeviceOrientationDidChangeNotification
object:nil];
2) Définir ce code pour intercepter la notification après la rotation:
-(void)detectOrientation{
switch ([[UIDevice currentDevice] orientation]) {
case UIDeviceOrientationFaceDown:{
NSLog(@"UIDeviceOrientationFaceDown");
break;
}
case UIDeviceOrientationFaceUp:{
NSLog(@"UIDeviceOrientationFaceUp");
break;
}
case UIDeviceOrientationLandscapeLeft:{
NSLog(@"UIDeviceOrientationLandscapeLeft");
break;
}
case UIDeviceOrientationLandscapeRight:{
NSLog(@"UIDeviceOrientationLandscapeRight");
break;
}
case UIDeviceOrientationPortrait:{
NSLog(@"UIDeviceOrientationPortrait");
break;
}
case UIDeviceOrientationPortraitUpsideDown:{
NSLog(@"UIDeviceOrientationPortraitUpsideDown");
break;
}
case UIDeviceOrientationUnknown:{
NSLog(@"UIDeviceOrientationUnknown");
break;
}
default:{
break;
}
}
}
J'ai les mêmes problèmes avec
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
Cette méthode génère des notifications pour les sept états de position périphérique possible:
UIDeviceOrientationPortrait
UIDeviceOrientationPortraitUpsideDown
UIDeviceOrientationLandscapeLeft
UIDeviceOrientationLandscapeRight
UIDeviceOrientationFaceUp
UIDeviceOrientationFaceDown
UIDeviceOrientationUnknown
Mais je ne ai besoin les 4 premiers, bien voici mon approche pour résoudre ce problème (avec le code inclus): Comment gérer UIDeviceOrientation pour gérer des vues mises en page