动画时未捕获的异常:CALayer位置包含NaN

8

当我在iPad上运行应用程序时,出现以下错误:

未捕获异常:CALayer 位置包含NaN

同样的代码在iPhone上运行正常。

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
    AnnotationView *aV;

    for (aV in views) {
        // Don't pin drop if annotation is user location
        if ([aV.annotation isKindOfClass:[MKUserLocation class]]) {
            continue;
        }

        // Check if current annotation is inside visible map rect, else go to next one
        MKMapPoint point =  MKMapPointForCoordinate(aV.annotation.coordinate);
        if (!MKMapRectContainsPoint(mapViewResult.visibleMapRect, point)) {
            continue;
        }

        CGRect endFrame = aV.frame;

        // Move annotation out of view
        // I checked here, CGRectEmpty returns true here

        aV.frame = CGRectMake(aV.frame.origin.x, aV.frame.origin.y - self.view.frame.size.height, aV.frame.size.width, aV.frame.size.height);

        // Animate drop
        [UIView animateWithDuration:0.5 delay:0.04*[views indexOfObject:aV] options: UIViewAnimationOptionCurveLinear animations:^{

            aV.frame = endFrame;

            // Animate squash
        }completion:^(BOOL finished){
            if (finished) {
                [UIView animateWithDuration:0.05 animations:^{
                    aV.transform = CGAffineTransformMakeScale(1.0, 0.8);

                }completion:^(BOOL finished){
                    if (finished) {
                        [UIView animateWithDuration:0.1 animations:^{
                            aV.transform = CGAffineTransformIdentity;
                        }];
                    }
                }];
            }
        }];
    }
}

我知道,分配的框架包含NaN值,我使用CGRectEmpty函数进行检查,并将该框架设置为CGRectZero,但它不起作用。
我从这里获得了可工作的代码:https://dev59.com/THI-5IYBdhLWcg3wbXxN#7045916 回溯/调试日志
*** Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [594.947 nan]'
*** First throw call stack:
(
    0   CoreFoundation                      0x048551e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x034ad8e5 objc_exception_throw + 44
    2   CoreFoundation                      0x04854fbb +[NSException raise:format:] + 139
    3   QuartzCore                          0x01a1be1a _ZN2CA5Layer12set_positionERKNS_4Vec2IdEEb + 190
    4   QuartzCore                          0x01a1bfd9 -[CALayer setPosition:] + 68
    5   QuartzCore                          0x01a1c6df -[CALayer setFrame:] + 799
    6   UIKit                               0x01fbbb4d -[UIView(Geometry) setFrame:] + 302
    7   MyAppName                           0x0026b025 -[ResultMapViewController mapView:didAddAnnotationViews:] + 2133
    8   MapKit                              0x01e0449b -[MKMapView annotationManager:didAddAnnotationRepresentations:] + 272
    9   MapKit                              0x01e3b53c -[MKAnnotationManager updateVisibleAnnotations] + 2052
    10  Foundation                          0x02ed6de7 __NSFireTimer + 97
    11  CoreFoundation                      0x04813ac6 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 22
    12  CoreFoundation                      0x048134ad __CFRunLoopDoTimer + 1181
    13  CoreFoundation                      0x047fb538 __CFRunLoopRun + 1816
    14  CoreFoundation                      0x047fa9d3 CFRunLoopRunSpecific + 467
    15  CoreFoundation                      0x047fa7eb CFRunLoopRunInMode + 123
    16  GraphicsServices                    0x03ccd5ee GSEventRunModal + 192
    17  GraphicsServices                    0x03ccd42b GSEventRun + 104
    18  UIKit                               0x01f60f9b UIApplicationMain + 1225
    19  MyAppName                           0x0001ba3d main + 141
    20  MyAppName                           0x000027e5 start + 53
)
libc++abi.dylib: terminating with uncaught exception of type NSException

2
添加异常断点。前往“断点导航器”,点击左下角的“+”号,选择“添加异常断点”。运行应用程序以获取断点。当您触发异常断点时,点击调试继续几次,您将获得回溯和更多错误信息。发布此信息以及Xcode/调试器消息的精确副本。 - zaph
我曾经在非常小的时间间隔或非常小的动画增量中看到过这种行为 - 我怀疑最终的原因是某个值太小,以至于它被规范化为零,然后用于除法。 - marko
1个回答

9

天啊!我不知道我会犯这样“愚蠢”的错误。

没错,问题出在这句话相同的代码在iPhone上可以正常工作。我一直在收到错误信息CALayer position contains NaN,因为在我的自定义- (AnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id ) annotation方法中,我正在计算

annotationView.layer.anchorPoint = CGPointMake(0.5, 1.0 - (diffFromBottom * 1.0)/annotationView.image.size.height);
< p >由于我在iPad项目中忘记添加图钉图像,annotationView.image为空白,而在iPhone代码中有。因此被0除以并导致错误。

我设置断点,并使用 NSLog 打印得知,

1.0 - (diffFromBottom * 1.0)/annotationView.image.size.height 这个值被打印为 -inf。这导致应用程序崩溃。

基本上,如果你进行无限或零的除法操作,就会出现此错误,因此每当你遇到这种错误时,请先查看代码,看一下哪里执行了除法运算。


从我的案例来看,“nan”是由于除以零值导致的,因此会出现“nan”值。也许这对你有所帮助。 - Virus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接