iOS 应用程序在 [UIView animateKeyframesWithDuration :] 期间崩溃

标签 ios objective-c uiviewanimation

我有一个带有关键帧动画的 iOS 应用。最后一个关键帧即将结束时,我的应用程序崩溃并出现此错误:

 *** Terminating app due to uncaught exception 'NSRangeException', 
reason: '*** -[__NSArrayM objectAtIndex:]: index 4294967295 beyond 
bounds for empty array'

我怀疑这可能是无意中嵌套动画的问题。有人遇到过这样的问题吗?

如果我用普通动画 block 替换关键帧动画,应用程序不会崩溃。

在关键帧动画中,我更改了 ScrollView 的内容偏移量和其他一些内容。我添加了 objc 消息日志记录,但即使这样也没有给出为什么会发生这种情况的线索。

完整堆栈跟踪:

*** First throw call stack:
(
    0   CoreFoundation                      0x021b15e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x01b968b6 objc_exception_throw + 44
    2   CoreFoundation                      0x021524e6 -[__NSArrayM objectAtIndex:] + 246
    3   UIKit                               0x0095288d __35-[UIViewKeyframeAnimationState pop]_block_invoke_3 + 415
    4   CoreFoundation                      0x0222e446 __53-[__NSArrayM enumerateObjectsWithOptions:usingBlock:]_block_invoke + 102
    5   CoreFoundation                      0x0222e352 -[__NSArrayM enumerateObjectsWithOptions:usingBlock:] + 290
    6   CoreFoundation                      0x021ab0a5 -[NSArray enumerateObjectsUsingBlock:] + 53
    7   UIKit                               0x009524e7 __35-[UIViewKeyframeAnimationState pop]_block_invoke + 518
    8   CoreFoundation                      0x0223b13a __65-[__NSDictionaryM enumerateKeysAndObjectsWithOptions:usingBlock:]_block_invoke + 106
    9   CoreFoundation                      0x0223b03e -[__NSDictionaryM enumerateKeysAndObjectsWithOptions:usingBlock:] + 190
    10  CoreFoundation                      0x0219f525 -[NSDictionary enumerateKeysAndObjectsUsingBlock:] + 53
    11  UIKit                               0x00952268 -[UIViewKeyframeAnimationState pop] + 385
    12  UIKit                               0x0094f336 +[UIViewAnimationState popAnimationState] + 47
    13  UIKit                               0x0096729a +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 508
    14  UIKit                               0x009688b9 +[UIView(UIViewKeyframeAnimations) animateKeyframesWithDuration:delay:options:animations:completion:] + 163

更新:(相关代码)

我单步调试了调试器中的代码。动画 block 完成得很好。一旦我越过线进入完成 block ,就会发生崩溃。

只有当我将开始时间设置为 > 0 时它才会崩溃。

有问题的数组似乎是动画堆栈的一部分,而不是我自己的数组之一(在我的动画代码期间没有访问任何数组)。

有没有办法转储 UIView 动画状态?

[UIView animateKeyframesWithDuration:1.0 delay:0.0 options:0 animations:^{

        [UIView addKeyframeWithRelativeStartTime:0.1 relativeDuration:0.9 animations:^{

            snapshot.center = snapshotAwayCenter;
            snapshot.transform = rotateOutTransform;

            CGFloat pageOffset = ...// calculate page offset
            self.scrollView.contentOffset = CGPointMake(pageOffset, 0.0);

            // block returns (stepped through in debugger)
        }];

    } completion:^(BOOL finished){ // When stepping over this line, the crash happens
        // more stuff here
}];

最佳答案

崩溃不是由于 UIView 的 addKeyframeWithRelativeStartTime: 方法造成的。

这是由于您访问数组中的元素超过数组中的项目数而发生的。查看您的代码,我看不到您在哪里使用数组,但请放心,您正在某处使用它(可能是在您忽略发布的代码中)。

由于明显的访问索引:4294967295,您显然正在尝试访问索引 -1 处的元素,但它是无符号的,因此显示为 4294967295。

关于iOS 应用程序在 [UIView animateKeyframesWithDuration :] 期间崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22399434/

相关文章:

ios - UIImageView 保持空白

ios - Amazon AWS DynamoDB 帮助选择主键

iphone - 两个lat long ios之间距离的准确性

ios - 来自文件的 UIImage 始终为零

objective-c - 以编程方式在 UIView 中添加 UIScrollView

ios - 在 ios 中的动画期间,对 imageview 的手势不起作用

ios - 如何停止函数在 Swift 3.0 中运行

iphone - 如何将背景音乐同步到屏幕上的视觉效果?

ios - 如何解决 CALayer 异常?

ios - drawRect 绘制的内容是否可以使用 UIView 动画进行动画处理?