使用Objective C在iOS8中分离横向和纵向视图

时间:2015-01-28 20:10:27

标签: objective-c ios8 viewwilltransitiontosize

我使用以下代码在iOS7中拥有单独的横向和纵向视图:

-(void)didRotateFromInterfaceOrientation:    (UIInterfaceOrientation)fromInterfaceOrientation {
    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {

    CGRect currentBounds=self.view.bounds;

    if (iPadInt==0) {
        if (self.interfaceOrientation==UIInterfaceOrientationLandscapeRight) {
            self.view=self.landscapeQuestionView;
            self.view.transform=CGAffineTransformMakeRotation((kDeg2Rad)*(90));
        } else if (self.interfaceOrientation==UIInterfaceOrientationLandscapeLeft) {
            self.view=self.landscapeQuestionView;
            self.view.transform=CGAffineTransformMakeRotation((kDeg2Rad)*(-90));
        } else if (self.interfaceOrientation==UIInterfaceOrientationPortrait) {
            self.view=self.portraitQuestionView;
            self.view.transform=CGAffineTransformMakeRotation(0);
        } else if (self.interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {
            self.view=self.portraitQuestionView;
            self.view.transform=CGAffineTransformMakeRotation((kDeg2Rad)*(180));
        } self.view.bounds=currentBounds;
    } else if (iPadInt==1) {
        if (self.interfaceOrientation==UIInterfaceOrientationLandscapeRight) {
            self.view=self.iPadLandscapeQuestionView;
            self.view.transform=CGAffineTransformMakeRotation((kDeg2Rad)*(90));
        rotationInt=90;
        } else if (self.interfaceOrientation==UIInterfaceOrientationLandscapeLeft) {
            self.view=self.iPadLandscapeQuestionView;
            self.view.transform=CGAffineTransformMakeRotation((kDeg2Rad)*(-90));
             rotationInt=90;
        } else if (self.interfaceOrientation==UIInterfaceOrientationPortrait) {
            self.view=self.iPadPortraitQuestionView;
            self.view.transform=CGAffineTransformMakeRotation(0);
            rotationInt=0;
        } else if (self.interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown) {
            self.view=self.iPadPortraitQuestionView;
            self.view.transform=CGAffineTransformMakeRotation((kDeg2Rad)*(180));
            rotationInt=0;
        } self.view.bounds=currentBounds;
    }
}

我知道我应该在iOS8中使用viewWillTransitionToSize方法,但我无法弄清楚如何在该方法中获得这些不同的视图。提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

这里有一个主题:Rotation methods deprecated, equivalent of 'didRotateFromInterfaceOrientation'?

我认为在您的情况下,您可以像这样使用 viewWillTransitionToSize:

-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context)
     {
         //get orientation
         UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
         //check orientation
         if (orientation==UIInterfaceOrientationPortrait)
         {
             NSLog(@"portrait");
         }
         else if(orientation==UIInterfaceOrientationPortraitUpsideDown)
         {
             NSLog(@"upside-down");
         }
         else if(orientation==UIInterfaceOrientationLandscapeRight)
         {
             NSLog(@"landscape right");
         }
         else if(orientation==UIInterfaceOrientationLandscapeLeft)
         {
             NSLog(@"landscape left");
         }
     } completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
     {

     }];
}