Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone &引用;无限UIScrollView“;包含uiwebview_Iphone_Objective C_Uiwebview_Uiscrollview_Infinite Scroll - Fatal编程技术网

Iphone &引用;无限UIScrollView“;包含uiwebview

Iphone &引用;无限UIScrollView“;包含uiwebview,iphone,objective-c,uiwebview,uiscrollview,infinite-scroll,Iphone,Objective C,Uiwebview,Uiscrollview,Infinite Scroll,背景: // Sense when the UIScrollView stops accelerating - (void)scrollViewDidEndDecelerating:(UIScrollView *)sender { if(pScrollView.contentOffset.x > pScrollView.frame.size.width) { [self switchToNextPost]; } els

背景:

    // Sense when the UIScrollView stops accelerating
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)sender {
        if(pScrollView.contentOffset.x > pScrollView.frame.size.width) {
            [self switchToNextPost];
        } else if (pScrollView.contentOffset.x < pScrollView.frame.size.width) {
            [self switchToPreviousPost];
        }
    }

    // Update the currentPost variable and switch page 1
    - (void)switchToNextPost {
        [prefs setInteger:[[self getNextKey:nil] intValue] forKey:@"currentPost"];

        [self loadPostWithKey:[self getCurrentKey] atPage:[NSNumber numberWithInt:1] withViewController:currentPage];
    }

    // Update the currentPost variable and switch page 1
    - (void)switchToPreviousPost {
        [prefs setInteger:[[self getPreviousKey:nil] intValue] forKey:@"currentPost"];

        [self loadPostWithKey:[self getCurrentKey] atPage:[NSNumber numberWithInt:1] withViewController:currentPage];
    }

    // Custom delegate function. Scrolls UIScrollView to page 1 and then loads new content on page 1 and 3
    - (void)webViewFinishedLoading {
        [pScrollView scrollRectToVisible:CGRectMake(pScrollView.frame.size.width, 0, pScrollView.frame.size.width, pScrollView.frame.size.height) animated:NO];

        [self loadPostWithKey:[self getPreviousKey:nil] atPage:[NSNumber numberWithInt:0] withViewController:previousPage];
        [self loadPostWithKey:[self getNextKey:nil] atPage:[NSNumber numberWithInt:2] withViewController:nextPage];
    }
首先是背景。我使用宽度为三页(0、1和2)的UIScrollView创建了一个“无限滚动视图”,然后确保在不滚动时UIScrollView始终位于第1页的中心。当您滚动到第0页或第2页,并且UIScrollViewDelegate感觉到滚动结束时,所有三个页面的内容首先按照您滚动的方向切换,然后UIScrollView立即移回第1页,模拟您可以继续滚动无休止的页面的效果

起初,我使用UITextView来显示每个页面的文本内容,但很快我就希望在设置文本样式时有更灵活的方式。因此,我最终使用了UIWebView,其中加载了一个NSString,其中包含我希望显示的样式化HTML内容。这非常好用,正是我想要的样子

问题:

    // Sense when the UIScrollView stops accelerating
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)sender {
        if(pScrollView.contentOffset.x > pScrollView.frame.size.width) {
            [self switchToNextPost];
        } else if (pScrollView.contentOffset.x < pScrollView.frame.size.width) {
            [self switchToPreviousPost];
        }
    }

    // Update the currentPost variable and switch page 1
    - (void)switchToNextPost {
        [prefs setInteger:[[self getNextKey:nil] intValue] forKey:@"currentPost"];

        [self loadPostWithKey:[self getCurrentKey] atPage:[NSNumber numberWithInt:1] withViewController:currentPage];
    }

    // Update the currentPost variable and switch page 1
    - (void)switchToPreviousPost {
        [prefs setInteger:[[self getPreviousKey:nil] intValue] forKey:@"currentPost"];

        [self loadPostWithKey:[self getCurrentKey] atPage:[NSNumber numberWithInt:1] withViewController:currentPage];
    }

    // Custom delegate function. Scrolls UIScrollView to page 1 and then loads new content on page 1 and 3
    - (void)webViewFinishedLoading {
        [pScrollView scrollRectToVisible:CGRectMake(pScrollView.frame.size.width, 0, pScrollView.frame.size.width, pScrollView.frame.size.height) animated:NO];

        [self loadPostWithKey:[self getPreviousKey:nil] atPage:[NSNumber numberWithInt:0] withViewController:previousPage];
        [self loadPostWithKey:[self getNextKey:nil] atPage:[NSNumber numberWithInt:2] withViewController:nextPage];
    }
当然,在UIWebView中加载、解析和显示简单HTML页面所需的时间比在UITextView中加载静态文本所需的时间要长。因此,当您向任何方向滚动,滚动视图自动向后移动并切换页面内容时,您可以在显示新内容之前提示旧内容十分之一秒

我的解决方案(无效):

    // Sense when the UIScrollView stops accelerating
    - (void)scrollViewDidEndDecelerating:(UIScrollView *)sender {
        if(pScrollView.contentOffset.x > pScrollView.frame.size.width) {
            [self switchToNextPost];
        } else if (pScrollView.contentOffset.x < pScrollView.frame.size.width) {
            [self switchToPreviousPost];
        }
    }

    // Update the currentPost variable and switch page 1
    - (void)switchToNextPost {
        [prefs setInteger:[[self getNextKey:nil] intValue] forKey:@"currentPost"];

        [self loadPostWithKey:[self getCurrentKey] atPage:[NSNumber numberWithInt:1] withViewController:currentPage];
    }

    // Update the currentPost variable and switch page 1
    - (void)switchToPreviousPost {
        [prefs setInteger:[[self getPreviousKey:nil] intValue] forKey:@"currentPost"];

        [self loadPostWithKey:[self getCurrentKey] atPage:[NSNumber numberWithInt:1] withViewController:currentPage];
    }

    // Custom delegate function. Scrolls UIScrollView to page 1 and then loads new content on page 1 and 3
    - (void)webViewFinishedLoading {
        [pScrollView scrollRectToVisible:CGRectMake(pScrollView.frame.size.width, 0, pScrollView.frame.size.width, pScrollView.frame.size.height) animated:NO];

        [self loadPostWithKey:[self getPreviousKey:nil] atPage:[NSNumber numberWithInt:0] withViewController:previousPage];
        [self loadPostWithKey:[self getNextKey:nil] atPage:[NSNumber numberWithInt:2] withViewController:nextPage];
    }
我在文档中搜索UIWebViewDelegate,发现页面加载完成时发生了一个事件。我把这个连接起来做这个:

  • 用户从第1页滚动到第2页
  • UIScrollViewDelegate调用ScrollViewDiEndDecelling,告诉第1页将其内容切换到第2页当前显示的相同内容
  • 第1页的UIWebViewDelegate调用webViewDidFinishLoad,告诉UIScrollView移回第1页,然后切换第0页和第2页的内容
  • 在我看来,这可以解决这个问题,但显然还不够。因此,我请求你的帮助。有什么想法吗?值得一提的是,我在所有方法中都使用了NSLog,并确保调用了它们。如果还有什么,我很乐意回答你的任何问题

    提前谢谢

    包含UIScrollView的视图控制器:

        // Sense when the UIScrollView stops accelerating
        - (void)scrollViewDidEndDecelerating:(UIScrollView *)sender {
            if(pScrollView.contentOffset.x > pScrollView.frame.size.width) {
                [self switchToNextPost];
            } else if (pScrollView.contentOffset.x < pScrollView.frame.size.width) {
                [self switchToPreviousPost];
            }
        }
    
        // Update the currentPost variable and switch page 1
        - (void)switchToNextPost {
            [prefs setInteger:[[self getNextKey:nil] intValue] forKey:@"currentPost"];
    
            [self loadPostWithKey:[self getCurrentKey] atPage:[NSNumber numberWithInt:1] withViewController:currentPage];
        }
    
        // Update the currentPost variable and switch page 1
        - (void)switchToPreviousPost {
            [prefs setInteger:[[self getPreviousKey:nil] intValue] forKey:@"currentPost"];
    
            [self loadPostWithKey:[self getCurrentKey] atPage:[NSNumber numberWithInt:1] withViewController:currentPage];
        }
    
        // Custom delegate function. Scrolls UIScrollView to page 1 and then loads new content on page 1 and 3
        - (void)webViewFinishedLoading {
            [pScrollView scrollRectToVisible:CGRectMake(pScrollView.frame.size.width, 0, pScrollView.frame.size.width, pScrollView.frame.size.height) animated:NO];
    
            [self loadPostWithKey:[self getPreviousKey:nil] atPage:[NSNumber numberWithInt:0] withViewController:previousPage];
            [self loadPostWithKey:[self getNextKey:nil] atPage:[NSNumber numberWithInt:2] withViewController:nextPage];
        }
    

    我想你能做的是

  • 从一开始就用相关内容填充所有三个页面。。(你可能已经在做了)
  • 开始滚动时,滚动时会显示下一页(或上一页)
  • 滚动完成后,将“滚动内容”视图移动到中心位置(与之前一样),而不是更改每个web视图中的文本,而是更改视图的框架,以使相关web视图(即一个用户已滚动到的视图)保持在中心位置,并在两侧调整其他两个视图
  • 更改两个web视图(不可见)的内容,以便为下一次滚动做好准备
  • 在快速滚动的情况下,这仍然会失败。。但由于加载内容的滞后,我认为任何方法都会在那里归档(我想)
    因此,您可以将所有web视图设置为空白(不加载任何内容),并在快速滚动时显示活动指示器。

    谢谢您的回答!事实上,一开始我很想使用这种方法,但后来我改变了主意。我最终没有使用它的原因是,我无法想出一个好方法来跟踪不同视图的位置。现在,我可以很容易地使用变量名previousPost、currentPost和nextPost来知道在哪个子视图中加载哪个帖子,但是当我开始移动它们时,我需要另一种方法来跟踪它。您将如何做到这一点?找到一个解决方案,使用您的技术跟踪子视图。现在它完美无缺了。再次感谢您的回答!你好,Alexander和lukya,我也在实现同样的事情。但无法解决这个问题。所以你们中的任何人都可以帮助我。我在等你的答复。我急需它。请帮助我尽快通过任何教程或任何工作代码。提前谢谢。你好,亚历山大和卢卡亚,我也在实现同样的事情。但无法解决这个问题。所以你们中的任何人都可以帮助我。我在等你的答复。我急需它。请帮助我尽快通过任何教程或任何工作代码。提前谢谢。