提前致谢
我想检测设备中方向何时发生变化... 因为我使用了这个听众
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
和方向改变方法是
- (void) orientationChanged:(id)obj
{
if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight )
{
self.view=self.landscapeView;
NSLog(@"landscapeView");
//[self loadView1];
}
else
{
self.view = self.portraitView;
NSLog(@"portraitView");
//[self loadView1];
}
}
每次进入ViewDidLoad()方法的方向......
任何人都可以帮助我..
答案 0 :(得分:6)
你的问题很模糊,这是一般性的解释。您可以覆盖下面提到的方法来处理方向更改
超越下面提到的方法
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// return YES to supported orientation.
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
/* Subclasses may override this method to perform additional actions immediately
prior to the rotation. For example, you might use this method to disable view
interactions, stop media playback, or temporarily turn off expensive drawing or live
updates. You might also use it to swap the current view for one that reflects the new
interface orientation. When this method is called, the interfaceOrientation property
still contains the view’s original orientation.
*/
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
/*Subclasses may override this method to perform additional actions immediately after
the rotation. For example, you might use this method to reenable view interactions,
start media playback again, or turn on expensive drawing or live updates. By the time
this method is called, the interfaceOrientation property is already set to the new
orientation.*/
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
/*This method is called from within the animation block used to rotate the view.
You can override this method and use it to configure additional animations that should
occur during the view rotation. For example, you could use it to adjust the zoom level
of your content, change the scroller position, or modify other animatable properties
of your view.*/
}
请勿在viewDidLoad或viewDidAppear中检查设备方向,如下所示
if( [UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeLeft)
OR if(UIInterfaceOrientationIsLandscape([UIDevice currentDevice].orientation))
因为很多时候它会产生意想不到的结果,就像iPhone / iPad放在桌子上一样(我经历过这个......非常讨厌的问题)。
此apple developer link将提供有关处理方向的更多信息
答案 1 :(得分:1)
更改以下内容
- (void) orientationChanged:(id)obj
{
if( [UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeLeft || [UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeRight )
{
self.view=self.landscapeView;
NSLog(@"landscapeView");
//[self loadView1];
}
else
{
self.view = self.portraitView;
NSLog(@"portraitView");
//[self loadView1];
}
}
答案 2 :(得分:1)
Try this
[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
if ( ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft) || ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight) )
{
//code here
}
答案 3 :(得分:0)
为什么不使用shouldAutorotateToInterfaceOrientation
?
它是UIViewController中已被调用的方法......