屏幕旋转

设置是否允许屏幕旋转

- shouldAutorotate{

    return YES;

}

设置可旋转的方向

- supportedInterfaceOrientations{

    return UIInterfaceOrientationMaskAll;

}

 iOS8之前的屏幕旋转的方法,之后已经被弃用,但是现在依然可以使用.

//当将要开始旋转的时候触发

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{

    //该方法中,一般做暂停音乐,关闭用户交互等操作

    NSLog(@"将要开始旋转了");

}

//旋转完成时触发

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{

    //该方法中,一般做重新开启音乐,打开用户交互等操作

    NSLog(@"已经完成屏幕旋转了");

}

//当开始做旋转动画时触发

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{

    //可以自定义旋转的动画效果

}

#warning iOS8之后使用这个方法代替

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator{

    

}

当屏幕旋转时会触发layoutSubviews方法,可以在此方法中重新布局子视图

//当视图的子视图的位置将要发生变化的时候会调用该方法重新布局视图的子视图.

//当对子视图设置frame时与将子视图放到父视图上时,系统分别调用layoutSubviews方法.

- (void)layoutSubviews{

    //获取手机状态栏的位置,以判断应用程序当前的位置.

    UIInterfaceOrientation location = [UIApplication sharedApplication].statusBarOrientation; //单例

    switch (location) {

        case UIInterfaceOrientationUnknown: {

            NSLog(@"发生未知错误");

            break;

        }

        case UIInterfaceOrientationPortrait:

        case UIInterfaceOrientationPortraitUpsideDown: {

            self.btn.frameCGRectMake(kButtonSpaceLeft, kButtonSpaceTop, kButtonWidth, kButtonHight);

            break;

        }

        case UIInterfaceOrientationLandscapeLeft:

        case UIInterfaceOrientationLandscapeRight: {

//            CGRect frame = self.btn.frame;

            self.btn.frame = CGRectMake(kButtonNewSpaceLeft, kLabelSpaceTop, kButtonWidth, kButtonHight);

            break;

        }

        default: {

            break;

        }

    }

}

原文地址:https://www.cnblogs.com/lion-witcher/p/5155654.html