我只是尝试在我的shouldautorotate方法中添加一些打印语句,并注意到它检查了4次哪个有意义,但即使我没有从纵向切换到横向模式,
它返回肖像3次,第四次返回景观,即使我的模拟器不在风景中。
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft){
NSLog(@"landscape left");
}else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
NSLog(@"landscape right");
}else if(interfaceOrientation == UIInterfaceOrientationPortrait){
NSLog(@" portrait");
}else if(interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
NSLog(@"portrait upsidedown");
}
任何人都知道为什么?
答案 0 :(得分:1)
尝试将该代码放入didAutorotate
或willAutorotate
方法。 shouldAutorotate
只应返回YES或NO。
我理论上定期检查shouldAutorotate
,而didAutorotate
仅在检测到方向转换时被触发。
这是我用来检查的代码:
- (void) reOrient{
if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)){
} else {
}
}
这是我创建的名为reOrient
的方法,该方法是从didAutorotate
- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
[self reOrient];
}
只需确保在创建一个像reOrient这样的新方法时,你也可以在标题中声明它(我一直忘记在开始时),如下所示:
- (void)reOrient;