我需要检测设备何时处于纵向,这样我才能发出特殊的动画。但我不希望我的视图自动旋转。
当设备旋转为纵向时,如何覆盖自动旋转的视图?我的应用程序只需要在风景中显示它的视图,但似乎我也需要支持肖像,如果我想要能够检测到肖像的旋转。
发布于 2012-02-03 08:55:57
在加载应用程序或视图时尝试执行以下操作:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:[UIDevice currentDevice]];
然后添加以下方法:
- (void) orientationChanged:(NSNotification *)note
{
UIDevice * device = note.object;
switch(device.orientation)
{
case UIDeviceOrientationPortrait:
/* start special animation */
break;
case UIDeviceOrientationPortraitUpsideDown:
/* start special animation */
break;
default:
break;
};
}
以上操作将允许您在不启用视图自动旋转的情况下注册设备的方向更改。
备注
在iOS中的所有情况下,当您添加观察者时,也要在适当的时间删除它(可能,但并非总是,当视图出现/消失时)。你只能有“对”的观察/不观察代码。如果你不这样做,应用程序将崩溃。选择在何处观察/不观察超出了本QA的范围。然而,你必须有一个"unobserve“来匹配上面的"observe”代码。
发布于 2015-07-20 06:33:03
1)大卫答案的Swift版本2)如果你仍然想在没有方向改变的情况下检测方向(Moe对How Do I detect the orientation of the device on iOS?的答案的Swift版本)
// Initial device orientation
let orientation: UIInterfaceOrientation = UIApplication.sharedApplication().statusBarOrientation
if(orientation == UIInterfaceOrientation.Unknown){
// code for Unknown
}
else if(orientation == UIInterfaceOrientation.Portrait){
// code for Portrait
}
else if(orientation == UIInterfaceOrientation.PortraitUpsideDown){
// code for Portrait
}
else if(orientation == UIInterfaceOrientation.LandscapeRight){
// code for Landscape
}
else if(orientation == UIInterfaceOrientation.LandscapeLeft){
// ode for Landscape
}
// To detect device orientation change
UIDevice.currentDevice().beginGeneratingDeviceOrientationNotifications()
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "orientationChanged:",
name: UIDeviceOrientationDidChangeNotification,
object: UIDevice.currentDevice())
orientationChanged函数
func orientationChanged(note: NSNotification)
{
let device: UIDevice = note.object as! UIDevice
switch(device.orientation)
{
case UIDeviceOrientation.Portrait:
// code for Portrait
break
case UIDeviceOrientation.PortraitUpsideDown:
// code for Portrait
break
case UIDeviceOrientation.LandscapeLeft:
// code for Landscape
break
case UIDeviceOrientation.LandscapeRight:
// code for Landscape
break
case UIDeviceOrientation.Unknown:
// code for Unknown
break
default:
break
}
}
发布于 2012-02-03 08:55:26
如果我没理解错的话,你的应用程序只是个景观应用。您可以简单地在应用程序设置中指定它是仅横向的,因此不需要担心旋转。无论iPad是如何定位的,这款应用程序都会从横向开始,并一直停留在那里。
https://stackoverflow.com/questions/9122149
复制相似问题