Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 是否有与didRotateFromInterfaceOrientation匹配的未弃用方法_Ios_Orientation Changes - Fatal编程技术网

Ios 是否有与didRotateFromInterfaceOrientation匹配的未弃用方法

Ios 是否有与didRotateFromInterfaceOrientation匹配的未弃用方法,ios,orientation-changes,Ios,Orientation Changes,我试图找到一个方法调用,一旦设备的方向改变,我就可以锁定它。是否有与didRotateFromInterfaceOrientation相同但未被弃用的内容?您可以随时注册UIDeviceOrientationIDChangeNotification [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRot

我试图找到一个方法调用,一旦设备的方向改变,我就可以锁定它。是否有与didRotateFromInterfaceOrientation相同但未被弃用的内容?

您可以随时注册UIDeviceOrientationIDChangeNotification

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didRotate:)
                                                 name:UIDeviceOrientationDidChangeNotification object:nil];

- (void) didRotate:(id)sender
{
    UIInterfaceOrientation io = [[UIApplication sharedApplication] statusBarOrientation];


    ...

从iOS 8开始,所有UIViewController都继承UIContentContainer协议,其中一个方法是
-(void)viewwillTransitionOnSize:(CGSize)size with TransitionOnCoordinator:(id)coordinator
,您可以(简单地)这样覆盖它:

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator
{
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];

    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {

        // Stuff you used to do in willRotateToInterfaceOrientation would go here.
        // If you don't need anything special, you can set this block to nil.

    } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {

        // Stuff you used to do in didRotateFromInterfaceOrientation would go here.
        // If not needed, set to nil.

    }];
}
-(void)视图将转换大小:(CGSize)带有转换协调器的大小:(id)协调器
{
[super ViewWillTransitionSize:size with TransitionCoordinator:coordinator];
[协调员AnimateLongsideTransition:^(id上下文){
//你以前在willRotateToInterfaceOrientation中做的事情会在这里进行。
//如果您不需要任何特殊功能,可以将此块设置为零。
}完成:^(id上下文){
//你以前在didRotateFromInterfaceOrientation中做的事情都会在这里。
//如果不需要,则设置为零。
}];
}

你会注意到方向并没有什么特别的,这是设计的;iOS视图旋转基本上是特定平移矩阵操作(将视图从一个纵横比调整为另一个纵横比只是一般调整操作的一种特定情况,其中源视图和目标视图的大小事先已知)和旋转矩阵操作(由OS处理)的组合

Swift 3全套奇洛答案的版本:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    coordinator.animate(alongsideTransition: { context in
            context.viewController(forKey: UITransitionContextViewControllerKey.from)
            // Stuff you used to do in willRotateToInterfaceOrientation would go here.
            // If you don't need anything special, you can set this block to nil. 
        }, completion: { context in
            // Stuff you used to do in didRotateFromInterfaceOrientation would go here.
            // If not needed, set to nil.
        })
}

这就是我要找的。谢谢谢谢你的回答,迈克。这同样有效,但我更喜欢遵守协议而不是使用通知。