2011-12-12 86 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我只更正了这个答案的格式。 –

相关问题