UIViewController动画等到动画完成
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UIViewController动画等到动画完成相关的知识,希望对你有一定的参考价值。
我正在使用这个观察者:UIDeviceOrientationDidChangeNotification
来检测用户何时更改设备方向。当方向改变为风景时,我提出了一个新的UIViewController
或当他将它改回肖像时解雇这个UIViewController
。
我的问题开始时,用户开始旋转设备的次数和速度,然后应用程序疯狂,直到我收到此错误:
警告:尝试显示其视图不在窗口层次结构中!`。
等待动画结束然后更改旋转的最佳方法是什么?
这是我在Presenting视图控制器上使用的:
- (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视图控制器上使用的内容:
- (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];
看你是否应该开始另一个动画。
以上是关于UIViewController动画等到动画完成的主要内容,如果未能解决你的问题,请参考以下文章
jQuery ajax,等到 beforeSend 动画完成