not*_*oop 0 iphone cocoa-touch
我想按顺序呈现多个模态视图(例如,从图像选择器中选择图片后显示确认页面).我的问题是解雇动画并在后续步骤中无延迟地呈现总是会使应用程序崩溃EXC_BAD_ACCESS.
我假设问题是CoreAnimation不区分两个转换并且无法正确检测第一个转换是否已经结束.
到目前为止,我的工作是引入1段延迟,这似乎解决了这个问题.但是,我认为这会使代码有点脆弱.还有另一种解决方法吗?
这是UIKit中的错误吗?我应该提交错误报告吗?
示例代码
这是一个重现崩溃的简单案例:
使用以下类创建一个新的基于视图的项目作为主控制器的实现
显示图像选择器视图时点击"取消"
预期的行为:由于随后的调用,选择器视图被解除并再次显示viewDidAppear.
实际行为:它与下面显示的堆栈跟踪崩溃.
码:
#import "SampleViewController.h"
@implementation SampleViewController
- (void)showModal {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
[self presentModalViewController:picker animated:YES];
// [picker release];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self showModal]; // this line crashes the app
// the following works as desired
// [self performSelector:@selector(showModal) withObject:nil afterDelay:1];
}
@end
Run Code Online (Sandbox Code Playgroud)
崩溃堆栈跟踪:
#0 0x30b43212 in -[UIWindowController transitionViewDidComplete:fromView:toView:] #1 0x3095828e in -[UITransitionView notifyDidCompleteTransition:] #2 0x3091af0d in -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] #3 0x3091ad7c in -[UIViewAnimationState animationDidStop:finished:] #4 0x00b54331 in run_animation_callbacks #5 0x00b54109 in CA::timer_callback #6 0x302454a0 in CFRunLoopRunSpecific #7 0x30244628 in CFRunLoopRunInMode #8 0x32044c31 in GSEventRunModal #9 0x32044cf6 in GSEventRun #10 0x309021ee in UIApplicationMain #11 0x00002794 in main at main.m:14
您需要让动画上下文完成.你已经发现了
[self performSelector:@selector(showModal)withObject:nil afterDelay:1];
工作,但显然,延迟是不好的,所以这样做:
[self performSelector:@selector(showModal) withObject:nil afterDelay:0.0];
Run Code Online (Sandbox Code Playgroud)
当你使用afterDelay:0.0时,它不会直接调用选择器,而是将你的runloop上的调用排入队列,这样你的所有状态(自动释放池,动画上下文等)都可以正确地使用,然后在runloop启动时立即调用你的调用处理事件.
可能会出现问题的一个问题是用户可以通过点击屏幕来让UIEvents关闭,但你可以通过在动画启动之前调用它来解决这个问题.
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
Run Code Online (Sandbox Code Playgroud)
这一旦你在屏幕上有你的最终模态
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
Run Code Online (Sandbox Code Playgroud)
通常,您希望在动画快速过渡时花费UI交互.
| 归档时间: |
|
| 查看次数: |
1932 次 |
| 最近记录: |