我该如何解决CALayer异常?

时间:2013-09-06 03:27:36

标签: ios objective-c uiwebview uiscrollview calayer

我的应用程序是一个简单的新闻应用程序,它包含一个部分按钮。当我们点击leftside上的按钮时,它将显示一个包含大量项目的popover(如体育,政治,地区新闻,国际新闻,游戏等)当我点击这些项目时,它将从xml加载内容并显示到webview。

但是,我尝试点击一些项目,例如最多3到4,继续应用程序崩溃。应用程序仅在ipad 1(iOS 5.1)和ipad 3中崩溃,其工作正常(iOS6.1.3)。当我在模拟器中运行应用程序时,我收到了崩溃日志。

CRASH LOGS

*** Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [nan nan]' 
*** First throw call stack: 
(0x236e022 0x1802cd6 0x2316a48 0x23169b9 0x7c5c0d 0x7bbf55 0x1a227d3 0xb03fae 0xd0eff3 0xb0307e 0x236fdea 0x22d97f1 0x9be27d 0x9aa9ae 0x9bc86f 0x9ab70f 0x9abd07 0x10aa7 0x9f65c5 0x9f67fa 0x128b85d 0x2342936 0x23423d7 0x22a5790 0x22a4d84 0x22a4c9b 0x1dfb7d8 0x1dfb88a 0x965626 0x22cd 0x21f5) 
terminate called throwing an exception(lldb)

下图显示了简单的图纸

enter image description here

webview放在滚动视图中

请检查以下代码

-(void)loadpages:(int)page{

    if((page>links.count-1)||(page<0))
        return;


        NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL fileURLWithPath:HTMLPath]];

    if(([[[[[scrollView subviews] objectAtIndex:page] request] URL] absoluteString] ==nil)||(page==0)||(page==31))
    {

           [[[self.scrollView subviews] objectAtIndex:page] loadRequest:request];

    }else if([[[[[[scrollView subviews] objectAtIndex:page] request] URL] absoluteString] isEqualToString:@"about:blank"]){

           [[[self.scrollView subviews] objectAtIndex:page] loadRequest:request];

    }

}






- (void)scrollViewDidScroll:(UIScrollView *)sender {

    if (sender==scrollView) {
        // Update the page when more than 50% of the previous/next page is visible
        CGFloat pageWidth = scrollView.frame.size.width;
        int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
        if(currentPage!= page){
            currentPage = page;
            // NSLog(@"currentpage:%d",currentPage);

            NSDictionary * news = (NSDictionary *)[links objectAtIndex:page];
            if (page==0) {
                Title_buttonItem.hidden=NO;
                [Title_lbl setText:nil];
                [Title_buttonItem setTitle:nil forState:UIControlStateNormal];
                [Title_buttonItem setImage:[UIImage imageNamed:@"Logo1.png"] forState:UIControlStateNormal];
            }else{

                [Title_lbl setText:[news objectForKey:@"title"]];
                Title_buttonItem.hidden=YES;
            }

            if (_lastContentOffset < (int)sender.contentOffset.x) {
                // moved right
                if (currentPage==31) {
                    TwoPage=currentPage;
                    allpage=0;
                    [self loadpages:currentPage];
                } else {
                    TwoPage=currentPage;
                    allpage=0;
                    [self loadpages:currentPage];
                    [self loadpages:currentPage+1];
                }

            }
            else {
                // moved left
                TwoPage=0;

                if (currentPage==0) {
                    allpage=currentPage;
                    [self loadpages:currentPage];
                }else{
                    allpage=currentPage;
                    [self loadpages:currentPage];
                    [self loadpages:currentPage-1];
                }

            }

            if ([Checking_DistrictPage isEqualToString:@"YES"]) {

                if (currentPage<([Title_Sections count]-[Title_Districts count])) {

                    frame.origin.x = (self.scrollView.frame.size.width) * MovePage;
                    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * [links count],self.scrollView.frame.size.height);
                    [scrollView scrollRectToVisible:CGRectMake(([Title_Sections count]-[Title_Districts count])*scrollView.frame.size.width, 0, scrollView.frame.size.width, scrollView.frame.size.height) animated:NO];
                    //[[[self.scrollView subviews] objectAtIndex:page] loadRequest:request];

                }else{
                    frame.origin.x = (self.scrollView.frame.size.width) * MovePage;
                    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * [links count],self.scrollView.frame.size.height);
//                    if (Check_district==YES) {
//                        splashImage =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 1024, 768)];
//                        [splashImage setImage:[UIImage imageNamed:@"Default-Landscape.png"]];
//                        [self.tabBarController.view addSubview:splashImage];
//                        [act startAnimating];
//                        [splashImage addSubview:act];
//                        timer=[NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(RemovingSplash) userInfo:nil repeats:NO];
//                        [self startDistrictfeedstore];
//                        
//                    }

                }
            }
            else{
                NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL fileURLWithPath:HTMLPath]];

                [[[self.scrollView subviews] objectAtIndex:page] loadRequest:request];

            }

        }
    }
    //NSLog(@"scrollview did scroll");

}





- (BOOL)webView:(UIWebView *)webView1 shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    if (webView1==AD_webview) {


    }else{
        NSString *requestString = [[request URL] absoluteString];
       if ([requestString hasPrefix:@"mb::"])
         {
            NSArray *components = [requestString componentsSeparatedByString:@"::"];
             NSString *functionCall = [[[components objectAtIndex:1] componentsSeparatedByString:@"****"] objectAtIndex:0];
             NSString *args = [[[components objectAtIndex:1] componentsSeparatedByString:@"****"] objectAtIndex:1];
                args = [args stringByReplacingOccurrencesOfString:@"$" withString:@"'"];
             args = [args stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];


             [[NSNotificationCenter defaultCenter] postNotificationName:functionCall object:args];


             return NO;
         }
    }
}

1 个答案:

答案 0 :(得分:1)

NaN表示“非数字”。您有时也可能会看到inf,代表“无限”。当浮点计算导致无穷大或未定义值时,会发生这种情况。通常它是除以0错误或未初始化变量的结果。这意味着在某些时候,您将CALayer的位置设置为无效的浮点值。您可以通过调用标准C宏NaNs来检查isnan()的值,但根据this page,它不可移植。 FWIW,我好运使用它。 (还有一个用于检查无穷大的isinf()宏。)您可能也很幸运使用静态分析器来查看是否有任何未初始化的变量。 (或者如果未初始化的变量尚未打开,则打开编译器警告。)