2010-12-07 29 views
5

Mon écran d'accueil fonctionne, mais mon application fonctionne en mode paysage et l'écran d'accueil s'affiche en mode portrait par défaut.Comment faire pivoter l'écran de démarrage personnalisé sur iOS?

Comment puis-je démarrer l'application afin que l'écran de démarrage permute entre les modes paysage comme mon application?

J'utilise le code suivant:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
// Overriden to allow any orientation. 
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || 
    interfaceOrientation == UIInterfaceOrientationLandscapeRight) 
    return YES; 
else { 
    return NO; } 
} 

et pour l'écran d'accueil

-(void)displayScreen { 
UIViewController *displayViewController=[[UIViewController alloc] init]; 
displayViewController.view = displaySplashScreen; 
[self presentModalViewController:displayViewController animated:NO]; 
[self performSelector:@selector(removeScreen) withObject:nil afterDelay:3.0]; 
} 
-(void)removeScreen 
{ [[self modalViewController] dismissModalViewControllerAnimated:YES]; 
} 

Mais comment puis-je mettre la rotation à l'intérieur de l'écran d'affichage?

+0

Cette publication n'a vraiment rien à voir avec Xcode. Donc, j'ai enlevé ce mot de votre titre. –

+0

Tags ajoutés iphone et ipad, comme beaucoup de gens ont comme favoris et vous obtiendrez une meilleure vue –

Répondre

3

@zoul - aimer cette solution jusqu'à présent. Cependant, si/quand cette vue a des sous-vues, elles n'apparaissent pas. des idées?

mise à jour:

résolu ce problème en ajoutant un sous-vue au UIView créé en -startupImageWithOrientation:pas self.view.

- (UIView *)startupImageWithOrientation:(UIInterfaceOrientation)io{ 
    UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"Default-%@.png", UIInterfaceOrientationName(io)]]; 
    UIView *aView = [[UIImageView alloc] initWithImage:img]; 
    [aView setFrame:[[UIScreen mainScreen] applicationFrame]]; 

    // define the version number label 
    self.versionNumberLabel_iPadSplashScreen.text = [NSString stringWithFormat:@"Version %@", 
                      [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]]; 

    [aView addSubview:self.versionNumberLabel_iPadSplashScreen]; 

    return [aView autorelease]; 
} 
4

Aha. Si vous voulez afficher votre propre écran de démarrage, vous devez créer un contrôleur de vue spécial pour cela, ce que vous avez déjà fait. Je pense que vous pouvez simplifier le code de requête autorotation:

- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) foo 
{ 
    return YES; // all interface orientations supported 
} 

Maintenant, vous devez penser aux écrans de démarrage pour différentes orientations. Avez-vous une image splash séparée pour le paysage et le portrait? Si oui, vous pouvez faire quelque chose comme ceci:

- (UIView*) startupImageWithOrientation: (UIInterfaceOrientation) io 
{ 
    UIImage *img = [UIImage imageNamed:[NSString 
     stringWithFormat:@"Default-%@.png", UIInterfaceOrientationName(io)]]; 
    UIView *view = [[UIImageView alloc] initWithImage:img]; 
    [view setFrame:[[UIScreen mainScreen] applicationFrame]]; 
    return [view autorelease]; 
} 

- (void) loadView 
{ 
    self.view = [self startupImageWithOrientation:self.interfaceOrientation]; 
} 

- (void) willRotateToInterfaceOrientation: (UIInterfaceOrientation) io 
    duration: (NSTimeInterval) duration 
{ 
    self.view = [self startupImageWithOrientation:io]; 
    self.view.transform = CGAffineTransformFromUIOrientation(io); 
} 

Il y a deux fonctions utilitaires appelées, vous pouvez farcir ceux-ci dans un fichier séparé:

NSString *UIInterfaceOrientationName(UIInterfaceOrientation io) 
{ 
    return UIInterfaceOrientationIsPortrait(io) ? @"Portrait" : @"Landscape"; 
} 

CGAffineTransform CGAffineTransformFromUIOrientation(UIInterfaceOrientation io) 
{ 
    assert(io <= 4); 
    // unknown, portrait, portrait u/d, landscape L, landscape R 
    static float angles[] = {0, 0, M_PI, M_PI/2, -M_PI/2}; 
    return CGAffineTransformMakeRotation(angles[io]); 
} 

Il est un peu en désordre, je serais intéressé dans une solution plus simple moi-même.

1

La résolution de votre defult.png peut affecter l'orientation de votre application si vous avez des orientations verrouillées. Par exemple, si une image splash 1024x768 est utilisée et que la vue initiale ne prend pas en charge le mode portrait par des verrous d'orientation, cela peut provoquer l'apparition d'objets visuels hors écran (en particulier lorsque l'animation est impliquée). lui-même dans une configuration de portrait, même si l'appareil peut être en paysage.

Généralement, les images 1024x768 impliquent le portrait tandis que les images 768x1024 impliquent le paysage. Si cela ne suffit pas ou si vous souhaitez passer de l'image par défaut initiale à un écran de connexion, vous pouvez utiliser un contrôleur de vue pour "continuer" l'éclaboussement.

charge toutes les viewControllers normales dans la fenêtre, puis mettez votre « splash » viewController en alors dans votre méthode splashController utilisation shouldAutorotateToInterfaceOrientation pour définir l'image de droite (sur un ImageViewController):

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Overriden to allow any orientation. 

switch ([[UIDevice currentDevice] orientation]) 
{ 
    case UIInterfaceOrientationLandscapeRight: 
    splashImage.image = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"Default"] ofType:@"png"]]; 
    break; 
    case UIInterfaceOrientationPortrait: 
    splashImage.image = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"Default"] ofType:@"png"]]; 
    break; 
    default: 
    splashImage.image = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"Default2"] ofType:@"png"]]; 
    break; 
} 

    return UIInterfaceOrientationIsLandscape(interfaceOrientation); 
} 

La configuration du les images que j'ai utilisées peuvent ne pas vous convenir, donc une expérimentation pourrait être nécessaire.

0
- (void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 

    if (toInterfaceOrientation == UIInterfaceOrientationPortrait) 
    { 
     // take action here , when phone is "Portrait" 

    } 
    else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) 
    { 
     action 4 LandscapeLeft 

    } 
    else if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) 
    { 
     //PortraitUpsideDown 

    } 
    else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) 
    { 
     //LandscapeRight 

    } 

Notez que vous devez retourner la méthode OUI shouldAutorotateToInterfaceOrientation: