gpt4 book ai didi

ios - UIViewController 动画等到动画完成

转载 作者:搜寻专家 更新时间:2023-10-30 20:16:11 25 4
gpt4 key购买 nike

我正在使用此观察器:UIDeviceOrientationDidChangeNotification 来检测用户何时更改设备方向。当方向更改为横向时,我会显示一个新的 UIViewController 或在他将其更改回纵向时关闭此 UIViewController

当用户开始多次快速旋转设备时,我的问题就开始了,然后应用程序变得疯狂,直到我收到此错误:

Warning: Attempt to present on whose view is not in the window hierarchy!`.

等待动画结束然后更改旋转的最佳方法是什么?

这是我在呈现 View Controller 上使用的:

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self beginDeviceOrientationListener];
}

- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)beginDeviceOrientationListener
{
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:[UIDevice currentDevice]];
}

- (void)orientationChanged:(NSNotification *)notification
{
UIDevice *device = notification.object;
switch (device.orientation)
{
case UIDeviceOrientationLandscapeLeft:
case UIDeviceOrientationLandscapeRight:
{
TheViewControllerToPresent *viewController = [[TheViewControllerToPresent alloc] init];
[self presentViewController:viewController animated:YES completion:nil];
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeRight] forKey:@"orientation"];
[[UIApplication sharedApplication] setStatusBarOrientation:[[[UIDevice currentDevice] valueForKey:@"orientation"] integerValue] animated:YES];
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
}
break;

default:
break;
}
}

这是我在Presented view controller 上使用的:

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self beginDeviceOrientationListener];
}

- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)beginDeviceOrientationListener
{
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:[UIDevice currentDevice]];
}

- (void)orientationChanged:(NSNotification *)notification
{
UIDevice *device = notification.object;
switch (device.orientation)
{
case UIDeviceOrientationPortrait:
{
[self dismissViewControllerAnimated:YES completion:nil];
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"];
[[UIApplication sharedApplication] setStatusBarOrientation:[[[UIDevice currentDevice] valueForKey:@"orientation"] integerValue] animated:YES];
}
break;
default:
break;
}
}

最佳答案

我自己使用的最简单的解决方案是阻止用户在动画进行期间进行任何更改。

这是通过在动画开始时添加以下代码来完成的:

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

和完成处理程序:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[UIApplication sharedApplication] endIgnoringInteractionEvents];

用户可以旋转设备,但在动画期间不会生成事件,因此动画不会发生冲突。但是,当动画结束时,您应该明确检查方向:

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

看看你是否应该开始另一个动画。

关于ios - UIViewController 动画等到动画完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30097666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com