在纵向应用程序中旋转单个视图

时间:2013-11-20 09:10:18

标签: ios iphone rotation

我正在创建一个仅处于纵向模式的应用。但是包含图表视图,我想要在纵向和横向两个方向上的图表视图。我尝试了很多不同帖子的方法,但我无法旋转它。我正在使用iOS 6.任何帮助将不胜感激...

2 个答案:

答案 0 :(得分:1)

我的应用程序也仅支持纵向方向,但对于一个视图,我提供纵向和横向。

您需要访问视图的图层并手动旋转它。 添加以下代码并从orientation delegate方法调用它  - (void) didRotate:(NSNotification *)notification

这是代码..

- (void) rotateViewToOrientation:(UIInterfaceOrientation)interfaceOrientation
{ 
    CALayer *viewLayer = // assign view layer here.

    switch (interfaceOrientation)
    {
        case UIInterfaceOrientationPortrait:
        {
            viewLayer.transform = CATransform3DMakeRotation(0, 0.0, 0.0, 1.0);
            break;
        }
        case UIInterfaceOrientationPortraitUpsideDown:
        {
            viewLayer.transform = CATransform3DMakeRotation(180.0 / 180.0 * M_PI, 0.0, 0.0, 1.0);
            break;
        }
        case UIInterfaceOrientationLandscapeLeft:
        {
            viewLayer.transform = CATransform3DMakeRotation(90.0 / 180.0 * M_PI, 0.0, 0.0, 1.0);

            break;
        }
        case UIInterfaceOrientationLandscapeRight:
        {
            viewLayer.transform = CATransform3DMakeRotation(270.0 / 180.0 * M_PI, 0.0, 0.0, 1.0);
            break;
        }

        default:
            break;
    }
}

转换后根据要求设置帧。

答案 1 :(得分:0)

首先,您的目标设置应如下所示:

Supported Interface Orientations

在UITabBarController中:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // You do not need this method if you are not supporting earlier iOS Versions
    return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

-(NSUInteger)supportedInterfaceOrientations
{
    if (self.selectedViewController) 
    return [self.selectedViewController supportedInterfaceOrientations];

    return UIInterfaceOrientationMaskPortrait;
}

-(BOOL)shouldAutorotate
{
    return YES;
}

在ViewController中:

a)如果您不想轮换:

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
 {
     return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }

 - (BOOL)shouldAutorotate
 {
     return NO;
 }

 - (NSUInteger)supportedInterfaceOrientations
 {
     return UIInterfaceOrientationMaskPortrait;
 }

b)如果你想旋转到风景:

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
 {
   return YES;
 }

 - (BOOL)shouldAutorotate
 {
      return YES;
 }

 - (NSInteger)supportedInterfaceOrientations
 {
      return UIInterfaceOrientationMaskAllButUpsideDown;
 }

其他解决方案是在AppDelegate中实现此方法:

 - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
 {
      NSUInteger orientations = UIInterfaceOrientationMaskAll;

     if (self.window.rootViewController) {
         UIViewController* presented = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
         orientations = [presented supportedInterfaceOrientations];
     }

     return orientations; 
 }