CALayerInvalidGeometry错误,原因是CALayer的边界包含NaN:[0 0; nan nan],在视图中崩溃。

24

我已经仔细研究了这个问题,但似乎只能获取与webView和表格相关的内容。我的问题似乎完全不同,但却出现了相同的崩溃异常:

CALayerInvalidGeometry', reason: 'CALayer bounds contains NaN: [0 0; nan nan]

我在这里有一个视图,可以淡入淡出和缩放图片。最近我决定改用CGAffineTransformScale在UIView动画中更改代码,而不是每次计时器滴答声时都将其放大一点。这样可以节省处理功率。

但是无论我以什么顺序放置图片,它总是在第19张后崩溃。它似乎不是指向位置坐标数组的问题,因为它的长度只有6,并且在达到长度后循环。因此,由于我实施了此动画代码,所以现在出现了这个崩溃。有人知道原因吗?

这是我自从开始崩溃以来更改的部分:

-(void) onTimer{

if(scaleTimer_A==5){

    imageView_A.image = [UIImage imageNamed:[attractList_A objectAtIndex:imageIndex_A]];

    imageView_A.frame = CGRectMake(300, 200, 3.86, 3.86);

    imageView_A.center = CGPointMake(attractLocs_x_A[attractLocs_A_index], attractLocs_y_A[attractLocs_A_index]);



    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:2];
    imageView_A.alpha = 1;
    imageView_A.transform = CGAffineTransformScale(imageView_A.transform, 100, 100);
    [UIView commitAnimations]; 
}

if(scaleTimer_A==10){

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:.75];
    imageView_A.alpha = 0;
    imageView_A.transform = CGAffineTransformScale(imageView_A.transform, 1.2, 1.2);
    [UIView commitAnimations]; 

    scaleTimer_A=0;

    imageIndex_A+=1;

    if(imageIndex_A==imageIndex_A_size){
        imageIndex_A=0;
    }

    attractLocs_A_index+=1;

    if(attractLocs_A_index==attractLocs_A_SIZE){
        NSLog(@"Back to zero A");
        attractLocs_A_index=0;
    }
    NSLog(@"Image A =%@", [attractList_A objectAtIndex:imageIndex_A]);
}

scaleTimer_A+=1;}

编辑:

以下是我如何使用CGAffineTransformIdentity使上述代码正常工作且避免崩溃问题。

-(void) onTimer{

if(scaleTimer_A==5){

    imageView_A.image = [UIImage imageNamed:[attractList_A objectAtIndex:imageIndex_A]];

    imageView_A.transform = CGAffineTransformIdentity;

    imageView_A.center = CGPointMake(attractLocs_x_A[attractLocs_A_index], attractLocs_y_A[attractLocs_A_index]);



    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:2];
    imageView_A.alpha = 1;
    imageView_A.transform = CGAffineTransformScale(CGAffineTransformIdentity, 100, 100);
    [UIView commitAnimations]; 
}

if(scaleTimer_A==10){

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:.75];
    imageView_A.alpha = 0;
    imageView_A.transform = CGAffineTransformScale(CGAffineTransformIdentity, 120, 120);
    [UIView commitAnimations]; 

    scaleTimer_A=0;

    imageIndex_A+=1;

    if(imageIndex_A==imageIndex_A_size){
        imageIndex_A=0;
    }

    attractLocs_A_index+=1;

    if(attractLocs_A_index==attractLocs_A_SIZE){
        NSLog(@"Back to zero A");
        attractLocs_A_index=0;
    }
    NSLog(@"Image A =%@", [attractList_A objectAtIndex:imageIndex_A]);
}

scaleTimer_A+=1;}

6
NaN通常发生在你除以零时。检查并仔细核对所有的数学计算。 - Aurum Aquila
我也听说过这个问题,但是在这段代码中我哪里除以了0? - Chewie The Chorkie
你能发布堆栈跟踪吗? - Max
糟糕..抱歉,那是完全错误的日志..稍等一下。 - Chewie The Chorkie
由于未捕获的异常'CALayerInvalidGeometry',应用程序终止,原因是:'CALayer bounds contains NaN: [0 0; nan nan]'。 *** 第一次抛出时的调用堆栈: ( 0 CoreFoundation 0x009ddbe9 __exceptionPreprocess + 185 1 libobjc.A.dylib 0x015a15c2 objc_exception_throw + 47 2 CoreFoundation 0x00996628 +[NSException raise:format:arguments:] + 136 3 CoreFoundation 0x0099659a +[NSException raise:format:] + 58 4 QuartzCore 0x0079a3ee _ZL16CALayerSetBoundsP7CALayerRKN2CA4RectEb + 227 - Chewie The Chorkie
显示剩余3条评论
1个回答

11

根据堆栈跟踪,问题出现在这里

imageView_A.frame = CGRectMake(300, 200, 3.86, 3.86);
尝试将 imageView_A.transform 设置为 identity。另一个解决方案(我认为更好)是使用 UIScrollView 进行缩放(也可以进行动画处理)。
编辑:尝试这个
    imageView_A.image = [UIImage imageNamed:[attractList_A objectAtIndex:imageIndex_A]];

    imageView_A.frame = CGRectMake(300, 200, 3.86, 3.86);

    imageView_A.center = CGPointMake(attractLocs_x_A[attractLocs_A_index], attractLocs_y_A[attractLocs_A_index]);



    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:2];
    imageView_A.alpha = 1;
    imageView_A.frame = CGRectMake(300, 200, 386.0f, 386.0f);
    [UIView commitAnimations]; 

在我的情况下,将imageView_A.transform设置为CGAffineTransformIdentity并不等同。UIScrollView对我来说并没有太多意义,特别是如果我想让多个图像视图独立变换。无论如何,为什么imageView_A的框架会有问题呢? - Chewie The Chorkie
是的,我已经尝试过了。缩放动画是不同的,具体取决于你的设置,第二次甚至可能无法显示。 - Chewie The Chorkie
请查看我在开头帖子中的编辑。我没有设置框架,而是使用了imageView_A.transform = CGAffineTransformIdentity; 谢谢。 - Chewie The Chorkie
@Max,你能帮我吗? - Navi
@Navi,我能为您做些什么? - Max
显示剩余5条评论

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