检测屏幕旋转

  // 判断当期设备的方向
// 屏幕旋转,view的大小发生变化,bounds会被修改,会自动调用view的方法.
//     布局
-(void)layoutSubviews{
//                                         方向
//[UIApplication sharedApplication].statusBarOrientation
       // 横屏    
    if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft ||[UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight) {
        _nameTextField.frame = CGRectMake(100, 10, 400, 30);
        _nameTextField.backgroundColor = [UIColor magentaColor];
    }else{
        // 竖屏
        _nameTextField.frame = CGRectMake(10, 50, 200, 30);
        _nameTextField.backgroundColor = [UIColor lightGrayColor];
    }

}

//设备支持的方向
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    
    return UIInterfaceOrientationMaskAll;
    
}
// 视图旋转触发的方法
-(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coordinator{

    UIView *backView = [self.view viewWithTag:666];
    
    //size是当前视图的size
//    if (size.width == 414) {
//        backView.backgroundColor = [UIColor redColor];
//    }else{
//        backView.backgroundColor = [UIColor greenColor];
//    }
//    NSLog(@"%@",NSStringFromCGSize(size));
    
    [coordinator animateAlongsideTransition:^(id  _Nonnull context) {
        //屏幕正在旋转的时候进行的操作
        backView.backgroundColor = [UIColor greenColor];
        // 获取屏幕旋转的时间
        NSLog(@"%lf",[coordinator transitionDuration]);
        
    } completion:^(id  _Nonnull context) {
        //屏幕旋转完成时进行的操作
        backView.backgroundColor = [UIColor purpleColor];
    
        
    }];

}

你可能感兴趣的:(检测屏幕旋转)