多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
### 【问题】 使用IOS9之前的SDK,转屏会有几个函数会被响应。 1.转屏前回调函数 - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)durationNS_DEPRECATED_IOS(3_0,8_0,"Implement viewWillTransitionToSize:withTransitionCoordinator: instead")__TVOS_PROHIBITED; 2.转屏后回调函数 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientationNS_DEPRECATED_IOS(2_0,8_0)__TVOS_PROHIBITED; 可是在IOS9的SDK中不能再使用了,那我们该如何处理呢? ### 【分析】 根据帮助文档的信息,让我们改用下面这个函数 - (void)viewWillTransitionToSize:([CGSize](https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGGeometry/index.html#//apple_ref/doc/c_ref/CGSize))`size `       withTransitionCoordinator:(id<[UIViewControllerTransitionCoordinator](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewControllerTransitionCoordinator_Protocol/index.html#//apple_ref/occ/intf/UIViewControllerTransitionCoordinator)>)`coordinator;` 可是怎么区分转屏前后呢? 我们看到第二个参数coordinator,它的类型是[UIViewControllerTransitionCoordinator](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewControllerTransitionCoordinator_Protocol/index.html#//apple_ref/occ/intf/UIViewControllerTransitionCoordinator),它能响应VC转换进度,并且提供了以下方法: - (BOOL)animateAlongsideTransition:(void (^)(id<[UIViewControllerTransitionCoordinatorContext](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewControllerTransitionCoordinatorContext_Protocol/index.html#//apple_ref/occ/intf/UIViewControllerTransitionCoordinatorContext)> context))`animation `                        completion:(void (^)(id<[UIViewControllerTransitionCoordinatorContext](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewControllerTransitionCoordinatorContext_Protocol/index.html#//apple_ref/occ/intf/UIViewControllerTransitionCoordinatorContext)> context))`completion;` ### 【解决方法】 ~~~ - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator { [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) { NSLog(@"转屏前调入"); } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) { NSLog(@"转屏后调入"); }]; [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; } ~~~