ios屏幕旋转

手机屏幕旋转时,试图控制器可以相应一些方法,可以再这些方法里自己布局视图


旋转方向的枚举类型

typedefNS_ENUM(NSInteger, UIInterfaceOrientation) {

   UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,

   UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,

   UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,

   UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft

};



是否支持旋转

- (BOOL)shouldAutorotate

{

NSLog(@"%s",__FUNCTION__);

returnNO;

}


在旋转某一个方向时,是否支持旋转

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

{

NSLog(@"%s",__FUNCTION__);

returnNO;

}




将要旋转时走的方法,可以判断要转向的方向

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

{

NSLog(@"%s  %f",__FUNCTION__,duration);

}


视图将要布局子视图

window调整显示的view controller的bounds,由于view controller的bounds发生变化,将会触发 viewWillLayoutSubviews 方法。

- (void)viewWillLayoutSubviews

{

NSLog(@"%s",__FUNCTION__);

}


视图完成布局子视图

- (void)viewDidLayoutSubviews

{

NSLog(@"%s",__FUNCTION__);

}


接着当前view controller的 willAnimateRotationToInterfaceOrientation:duration: 方法将会被调用。系统将会把该方法中执行的所有属性变化放到动animation block中。

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

{

NSLog(@"%s  %f",__FUNCTION__,duration);

}


旋转完成执行的方法

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

{

NSLog(@"%s",__FUNCTION__);

}