我有一个以UI的任何方向加载的视图。我要求允许用户选择并锁定他们想要的方向。下面是我的测试应用程序的图片。我想在选择Left时以编程方式将视图的方向更改为UIInterfaceOrientationLandscapeLeft。
我知道如何锁定它。我只是在尝试旋转视图时遇到问题,就好像设备是物理旋转的一样。
这是我的代码:
- (void)rotateView:(UIInterfaceOrientation)toInterfaceOrientation
{
self.interfaceOrientation = toInterfaceOrientation;
CGFloat rotationFactor = 0;
CGRect frame;
switch (toInterfaceOrientation) {
case UIInterfaceOrientationPortrait:
frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
rotationFactor = M_PI;
break;
case UIInterfaceOrientationLandscapeRight:
frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.height, self.view.frame.size.width);
rotationFactor = M_PI_2;
break;
case UIInterfaceOrientationLandscapeLeft:
frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.height, self.view.frame.size.width);
rotationFactor = 3 * M_PI_2;
break;
default:
break;
}
// check current orientation
if ([[UIApplication sharedApplication] statusBarOrientation] != toInterfaceOrientation) {
self.orientationLocked = NO;
// [[UIApplication sharedApplication] setStatusBarHidden:YES];
// [[UIApplication sharedApplication] setStatusBarOrientation:toInterfaceOrientation];
// [[UIApplication sharedApplication] setStatusBarHidden:NO];
// no, the orientation is wrong, we must rotate the UI
self.navigationController.view.userInteractionEnabled = NO;
[UIView beginAnimations:@"rotateView" context:NULL];
[UIView setAnimationDelegate:self];
// setup status bar
[[UIApplication sharedApplication] setStatusBarOrientation:toInterfaceOrientation animated:NO];
// rotate main view, in this sample the view of navigation controller is the root view in main window
[self.navigationController.view setTransform: CGAffineTransformMakeRotation(3 * M_PI_2)];
// set size of view
[self.navigationController.view setFrame:frame];
[UIView commitAnimations];
self.orientationLocked = YES;
}
}
我尝试过其他人的建议无济于事。我知道旋转值不正确。我只是想让视图立即旋转到任何旋转。
可能导致这些建议不起作用的唯一问题可能是因为此视图是根视图的子视图。有人可以帮助我以编程方式将此视图从纵向旋转到任何横向方向吗?
答案 0 :(得分:1)
嗯,试试新项目中的代码,我发布了你可能需要的整个代码,希望这有助于你...:)
在新项目中尝试这个
#import "AnyOrientationViewController.h"
@interface AnyOrientationViewController ()
{
}
@end
@implementation AnyOrientationViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.segmentControle.selectedSegmentIndex = 0;
[self.segmentControle addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
}
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIDeviceOrientationPortrait | UIDeviceOrientationLandscapeLeft | UIDeviceOrientationLandscapeRight;
}
- (void)valueChange:(UISegmentedControl *)sender
{
//hear is the code to change the orientation
int index = sender.selectedSegmentIndex;
switch (index) {
case 0:
NSLog(@"all orientation");
// [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait;
[UIApplication sharedApplication].statusBarOrientation = [[UIDevice currentDevice] orientation];
break;
case 1:
if([[UIApplication sharedApplication] statusBarOrientation] != UIInterfaceOrientationPortrait)
{
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait;
}
break;
case 2:
if([[UIApplication sharedApplication] statusBarOrientation] != UIInterfaceOrientationLandscapeLeft)
{
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeLeft;
}
break;
case 3:
if([[UIApplication sharedApplication] statusBarOrientation] != UIInterfaceOrientationLandscapeRight)
{
[UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;
}
break;
default:
break;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
答案 1 :(得分:1)
@Shan - 我使用你的代码来旋转状态栏并添加了一个调用以下方法来旋转视图。有用!!!!所以你让我走了一半。以下是代码的其余部分:
#define ROTATE_90 M_PI_2
#define ROTATE_180 M_PI
#define ROTATE_270 M_PI + M_PI_2
/**
* Rotates and resizes the view
*/
- (void)rotateToSelectedOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
CGRect viewRect = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
switch (toInterfaceOrientation) {
case UIInterfaceOrientationLandscapeLeft:
self.view.transform = CGAffineTransformMakeRotation(ROTATE_270);
break;
case UIInterfaceOrientationLandscapeRight:
self.view.transform = CGAffineTransformMakeRotation(ROTATE_90);
break;
case UIInterfaceOrientationPortrait:
self.view.transform = CGAffineTransformMakeRotation(0);
break;
case UIInterfaceOrientationPortraitUpsideDown:
break;
default:
self.view.transform = CGAffineTransformMakeRotation(0.0);
break;
}
if (UIInterfaceOrientationIsLandscape(self.currentOrientation) && UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
viewRect = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height);
} else {
viewRect = CGRectMake(0,0,self.view.frame.size.height,self.view.frame.size.width);
}
self.view.frame = viewRect;
self.currentOrientation = toInterfaceOrientation;
}
答案 2 :(得分:0)
如果您希望自己的应用自动定位给定视频以填满整个屏幕,无论宽高比(宽或高)和设备方向(纵向或横向) 试试这个:
注册状态栏方向更改时生成的通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotateDevice) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
将此代码添加到通知对象的选择器方法:
- (void)didRotateDevice
{
CGFloat theta = 0.0;
switch ([UIApplication sharedApplication].statusBarOrientation)
{
case UIDeviceOrientationPortrait:
theta += -90.0;
CGAffineTransform transform = self.transform;
transform = CGAffineTransformRotate(transform, radians(theta));
self.transform = transform;
break;
case UIDeviceOrientationLandscapeRight:
if (self.transform.a != 1.0) {
theta += 90.0;
transform = self.transform;
transform = CGAffineTransformRotate(transform, radians(theta));
self.transform = transform;
}
break;
case UIDeviceOrientationLandscapeLeft:
if (self.transform.a != 1.0) {
theta += -90.0;
transform = self.transform;
transform = CGAffineTransformRotate(transform, radians(theta));
self.transform = transform;
}
break;
default:
NSLog(@"default");
break;
}
[self setFrame:[[UIScreen mainScreen] bounds]];
}
只要设备的方向发生变化,上面的代码就会阻止视图旋转;所有其他视图将按预期旋转。