我拍摄了两个视图,一个是肖像和风景,我希望切换视图旋转我使用下面的代码,但它无法正常工作,我可能知道我哪里出错了,因为我是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 :(得分:1)
我在两个视图中显示图像一个水平一个,一个垂直....
在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
希望我的答案能帮助你...如果不是请告诉我你的身份我将发送你的项目。
答案 1 :(得分:0)
支持定位功能
-(Bool)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
现在要在旋转时执行操作,您必须使用以下功能
由于您有两个名为landscape和portrait的视图,请确保您已正确链接了引用插座
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
self.view = portrait;
}
else
{
self.view = landscape;
}
}
答案 2 :(得分: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;
}
}