2010-12-07 63 views
5

我的啓動畫面正在工作,但我的應用程序在橫向模式下工作,啓動畫面以默認肖像模式顯示。如何在iOS上旋轉自定義啓動畫面?

如何啓動應用程序,以便啓動屏幕在風景模式之間旋轉,如我的應用程序?

我用下面的代碼:

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

和閃屏

-(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]; 
} 

但我怎麼可以把顯示屏內的旋轉?

+0

這篇文章對Xcode確實沒什麼用處。所以,我已經從你的標題中刪除了這個詞。 – 2010-12-07 06:53:32

回答

3

@zoul - 迄今爲止喜歡這個解決方案。但是,如果/當該視圖有任何子視圖時 - 它們不顯示。有任何想法嗎?

更新:

通過增加一個子視圖中-startupImageWithOrientation: self.view創建的UIView修復了這個問題。

- (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

啊哈。如果你想顯示你自己的啓動畫面,你應該爲你創建一個特殊的視圖控制器,你已經做了。我認爲你可以簡化自動旋轉查詢代碼:

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

現在你必須考慮不同方向的啓動畫面。你有獨立的風景和肖像飛濺圖像嗎?如果是的話,你可以這樣做:

- (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); 
} 

有兩個叫,你可以的東西,這些成一個單獨的文件實用功能:

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]); 
} 

這是一個有點亂,我很有興趣更簡單的解決方案。

1

如果您已鎖定方向,則defult.png的解析會影響您的應用程序方向。

例如,如果使用1024x768閃屏圖像,並且初始視圖不支持通過方向鎖定進行肖像查看,則可能會導致視覺UI對象出現在屏幕外(特別是涉及動畫時),因爲視圖將嘗試呈現即使設備可能處於橫向狀態,它本身也處於縱向配置中。

一般而言,1024x768圖像意味着肖像,而768x1024圖像意味着風景。

如果這還不夠,或者您想要從初始默認圖像無縫地轉到例如登錄屏幕,那麼您可以使用視圖控制器來「繼續」飛濺。

加載一切正常viewControllers到窗口,然後把你的「撲通」的viewController中然後在splashController使用shouldAutorotateToInterfaceOrientation方法來設置正確的圖像(上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); 
} 

的配置我用過的圖片可能不適合你,所以可能需要一些實驗。

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 

    } 

請注意,您應該返回YES方法shouldAutorotateToInterfaceOrientation: