ios – 未捕获的异常:CALayer位置在执行动画时包含NaN

我在iPad上运行时在应用程序中收到此错误:

Uncaught exception: CALayer position contains 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://stackoverflow.com/a/7045916/1603234

Backtrace / Debug Log

*** 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

解决方法

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

是的,有问题的句子相同的代码在iPhone上运行正常.我得到错误CALayer位置包含NaN,因为在我的自定义 – (AnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)注释方法,我在哪里计算

annotationView.layer.anchorPoint = CGPointMake(0.5,1.0 - (diffFromBottom * 1.0)/annotationView.image.size.height);

annotationView.image是空白的,因为我忘了在iP​​ad项目中添加图钉图像. iPhone代码就在那里.所以它除以零(0)并导致错误.

我把断点,和NSLog打印了解,

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

基本上,如果你将无穷大或零执行除法,则会发生此错误,因此每当您遇到此类错误时,请首先查看执行除法运算的代码.

以上是来客网为你收集整理的ios – 未捕获的异常:CALayer位置在执行动画时包含NaN全部内容,希望文章能够帮你解决ios – 未捕获的异常:CALayer位置在执行动画时包含NaN所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。