使用objective-c以编程方式颠倒iOS应用程序
我想在ViewController中使用下面的代码来颠倒我的应用程序:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { //return NO; // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationMaskPortraitUpsideDown); } - (UIInterfaceOrientationMask) supportedInterfaceOrientations { return [super supportedInterfaceOrientations] | UIInterfaceOrientationMaskPortraitUpsideDown | UIInterfaceOrientationMaskPortrait; }
在AppDelegate中:
- (UIInterfaceOrientationMask) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { return UIInterfaceOrientationMaskPortraitUpsideDown | UIInterfaceOrientationMaskPortrait; }
当用户按下一个button
NSNumber * value = [NSNumber numberWithInt:UIInterfaceOrientationPortraitUpsideDown]; [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
该应用程序不知何故不旋转,我不知道为什么。 有人可以提供一个提示? 谢谢我在info.plist中添加了方向,并在部署信息部分的常规设置中设置了checkbox。
我是否正确理解你,你想以编程方式旋转用户界面,独立于设备的物理方向?
这不是如何工作的:当检测到设备的(物理)旋转时(例如,用户将其颠倒),由iOS调用委托方法(shouldAutorotate …等)。 在代表方法中,如果需要的话,您可以有机会将意见应用于新的方向。
在模拟器中,您可以通过Alt + Left / Alt + Right键模拟设备旋转。
您无法以编程方式旋转方向,这取决于设备的传感器。 但是,您可以旋转视图的图层。
self.view.layer.transform = CGAffineTransformMakeRotation(M_PI_2);