2011-12-12 112 views
0

我已經採取了兩個意見之一是肖像和風景,我想切換旋轉視圖我使用下面的代碼,但它不工作,我可以知道我哪裏錯了,因爲我是新來的iPhone開發。我沒有在ViewDidLoad方法中實現任何東西,我是否應該在那裏實現任何東西?縱向和橫向之間切換

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 

    if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) { 
     self.portrait.hidden = YES; 
     self.landscape.hidden = NO; 
    } else { 
     self.portrait.hidden = NO; 
     self.landscape.hidden =YES; 
    } 

    return YES; 
} 
+0

告訴我你的郵件我有一個很好的教程。 :) – mAc

回答

1

我有s在兩種意見之一在水平和一個立式howed將圖像.... A Sample View

在ViewController.m: -

@interface OrientationTutorialViewController : UIViewController 
{ 

    IBOutlet UIView *portraitView; 
    IBOutlet UIView *landscapeView; 

} 

@property (nonatomic, retain) IBOutlet UIView *portraitView; 
@property (nonatomic, retain) IBOutlet UIView *landscapeView; 


@end 

IN .H: -

@implementation OrientationTutorialViewController 

@synthesize portraitView, landscapeView; 


- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil]; 

} 

- (void) orientationChanged:(id)object 
{ 
    UIInterfaceOrientation interfaceOrientation = [[object object] orientation]; 

    if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) 
    { 
     self.view = self.portraitView; 
    } 
    else 
    { 
     self.view = self.landscapeView; 
    } 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return YES; 
} 



- (void)dealloc 
{ 
    [super dealloc]; 
} 

@end 

希望我答案幫助你...如果不是,請告訴我你的ID我會發送你的項目。

+0

嘿你收到我的郵件......這個答案幫助你.. ?? – mAc

0

爲了支持定位功能

-(Bool)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return YES; 

} 

現在執行的操作而旋轉,你必須使用下面的函數

當你有一個名爲橫向和縱向兩種觀點確保您已正確連接參考插座

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 


if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) 
{ 

    self.view = portrait; 



} 
else 
{ 
    self.view = landscape; 


} 

} 
+0

在景觀邏輯我正在寫self.view = self.landscape;和肖像反之亦然,但在旋轉它沒有得到改變我希望所有四個旋轉 – crazy2431

+0

在風景登錄你不必做self.view = self.landscape。改變視圖是由設備自動處理的。你應該確保你的所有ViewController都有「shouldAutorotateToInterfaceOrientation」函數,它應該只返回YES,其他編碼都不會。 –

+0

雅我知道that.Now我已經採取了兩個意見,並命名它有肖像和landscape.in我認爲我有一些UI設計都在視圖上完成。因此,我想旋轉的視圖分別切換意見。所以我應該添加上面的代碼。橫向和縱向邏輯。 – crazy2431

0
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) { 
     self.portrait.hidden = YES; 
     self.landscape.hidden = NO; 

     return YES; 
    } 
    else { 
     self.portrait.hidden = NO; 
     self.landscape.hidden = YES; 

     return YES; 
    } 
} 
+0

不,它不工作 – crazy2431

+0

@MisterJack但我有兩個視圖命名爲肖像和landscape.i想要切換他們旋轉 – crazy2431

+0

@ crazy2431我只更正了這個答案的格式。 –

相關問題