Tournez UIViewController pour contrer les changements dans UIInterfaceOrientation
-
21-09-2019 - |
Question
Je l'ai cherché beaucoup à ce sujet, et ne trouve rien pour me aider.
J'ai un UIViewController contenu dans un autre UIViewController. Lorsque la rotation de UIViewController parents, disent de Portrait à LandscapeLeft, je veux le faire paraître comme si l'enfant ne tournait pas. C'est-à-dire. Je veux que l'enfant ait la même orientation vers le ciel, peu importe l'orientation du parent. Si elle a un UIButton qui est debout dans Portrait, je veux le côté droit du bouton pour le « haut » dans UIInterfaceOrientationLandscapeLeft.
Est-ce possible? À l'heure actuelle, je fais des trucs vraiment dégoûtant comme ceci:
-(void) rotate:(UIInterfaceOrientation)fromOrientation: toOr:(UIInterfaceOrientation)toOrientation
{
if(((fromOrientation == UIInterfaceOrientationPortrait) && (toOrientation == UIInterfaceOrientationLandscapeRight))
|| ((fromOrientation == UIInterfaceOrientationPortraitUpsideDown) && (toOrientation == UIInterfaceOrientationLandscapeLeft)))
{
}
if(((fromOrientation == UIInterfaceOrientationLandscapeRight) && (toOrientation == UIInterfaceOrientationPortraitUpsideDown))
|| ((fromOrientation == UIInterfaceOrientationLandscapeLeft) && (toOrientation == UIInterfaceOrientationPortrait)))
{
}
if(((fromOrientation == UIInterfaceOrientationPortrait) && (toOrientation == UIInterfaceOrientationLandscapeLeft))
|| ((fromOrientation == UIInterfaceOrientationPortraitUpsideDown) && (toOrientation == UIInterfaceOrientationLandscapeRight)))
{
}
if(((fromOrientation == UIInterfaceOrientationLandscapeLeft) && (toOrientation == UIInterfaceOrientationPortraitUpsideDown))
|| ((fromOrientation == UIInterfaceOrientationLandscapeRight) && (toOrientation == UIInterfaceOrientationPortrait)))
{
}
if(((fromOrientation == UIInterfaceOrientationPortrait) && (toOrientation == UIInterfaceOrientationPortraitUpsideDown))
|| ((fromOrientation == UIInterfaceOrientationPortraitUpsideDown) && (toOrientation == UIInterfaceOrientationPortrait)))
{
}
if(((fromOrientation == UIInterfaceOrientationLandscapeLeft) && (toOrientation == UIInterfaceOrientationLandscapeRight))
|| ((fromOrientation == UIInterfaceOrientationLandscapeRight) && (toOrientation == UIInterfaceOrientationLandscapeLeft)))
{
}
}
qui semble être une très bonne perte de code. De plus, je comptais sur l'utilisation CGAffineTransform (comme cité ici: http://www.crystalminds.nl/ ? p = 1102 ) mais je suis confus de savoir si je dois changer les dimensions de la vue pour correspondre à ce qu'ils seront après la rotation.
Le grand cauchemar est ici que vous devez garder une trace d'une variable globale « d'orientation ». Si vous ne le faites pas, l'illusion est perdue et la ViewController est tournée dans tout.
Je pourrais vraiment utiliser un peu d'aide à ce sujet, merci!
La solution
La meilleure chose que vous pouvez faire est de changer les cadres de vos cadres de sous-vue selon les orientations ur d'interface. Vous pouvez le faire comme:
#pragma mark -
#pragma mark InterfaceOrientationMethods
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown || interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
//self.view = portraitView;
[self changeTheViewToPortrait:YES andDuration:duration];
}
else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){
//self.view = landscapeView;
[self changeTheViewToPortrait:NO andDuration:duration];
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------
- (void) changeTheViewToPortrait:(BOOL)portrait andDuration:(NSTimeInterval)duration{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
if(portrait){
//change the view and subview frames for the portrait view
}
else{
//change the view and subview frames for the landscape view
}
[UIView commitAnimations];
}
Hope this helps.
Autres conseils
i réalisé quelque chose .. disons que notre projet a couche multiple de ViewController (comme si vous ajoutez d'autres sous-vue contrôleur de vue sur votre contrôleur de vue)
willRotateToInterfaceOrientation: méthode de durée ne sera pas appelé à la 2ème couche de ViewController ...
ce que je l'ai été, après que j'INITIALISER ma 2ème couche vue contrôleur de mon plus haut sommet couche, puis quand willRotateToInterfaceOrientation: méthode de durée est appelée sur la partie supérieure couche la plus, je vais appeler willRotateToInterfaceOrientation: durée pour le contrôleur 2ème couche vue comme bien