Objective c PhotoScroller样本中的无限循环

Objective c PhotoScroller样本中的无限循环,objective-c,ios,photo-gallery,Objective C,Ios,Photo Gallery,因此,我正在修改苹果公司的PhotoScroller示例,并尝试添加一个无限循环功能,即:当用户滚动到最后一张图片时,下一张显示第一张图片 为此,我将contentSize宽度乘以100(以伪造无限循环),将最后需要的页面索引也乘以100,并使用伪造索引(索引模self.imageCount)显示正确的图像,如下所示: - (CGSize)contentSizeForPagingScrollView { // We have to use the paging scroll view's

因此,我正在修改苹果公司的PhotoScroller示例,并尝试添加一个无限循环功能,即:当用户滚动到最后一张图片时,下一张显示第一张图片

为此,我将contentSize宽度乘以100(以伪造无限循环),将最后需要的页面索引也乘以100,并使用伪造索引(索引模self.imageCount)显示正确的图像,如下所示:

- (CGSize)contentSizeForPagingScrollView {
    // We have to use the paging scroll view's bounds to calculate the contentSize, for the same reason outlined above.
    CGRect bounds = pagingScrollView.bounds;
    double rightBounds = bounds.size.height;
    // return CGSizeMake(bounds.size.width * [self imageCount], rightBounds); // no infinite loop
    return CGSizeMake(bounds.size.width * [self imageCount] * 100, rightBounds); // with infinite loop
}

- (void)tilePages 
{
    // Calculate which pages are visible
    CGRect visibleBounds = pagingScrollView.bounds;
    int firstNeededPageIndex = floorf(CGRectGetMinX(visibleBounds) / CGRectGetWidth(visibleBounds));
    int lastNeededPageIndex  = floorf((CGRectGetMaxX(visibleBounds)-1) / CGRectGetWidth(visibleBounds));
    firstNeededPageIndex = MAX(firstNeededPageIndex, 0);
    // lastNeededPageIndex  = MIN(lastNeededPageIndex, [self imageCount] - 1); // no infinite loop
    lastNeededPageIndex  = MIN(lastNeededPageIndex, [self imageCount] * 100 - 1); // with infinite loop

    // Recycle no-longer-visible pages 
    for (ImageScrollView *page in visiblePages) {
        if (page.index < firstNeededPageIndex || page.index > lastNeededPageIndex) {
            [recycledPages addObject:page];
            [page removeFromSuperview];
        }
    }

    [visiblePages minusSet:recycledPages];

    int fakeIndex = firstNeededPageIndex;
    // add missing pages
    for (int index = firstNeededPageIndex; index <= lastNeededPageIndex; index++) {
        fakeIndex = index % self.imageCount;
        if (![self isDisplayingPageForIndex:fakeIndex]) {
            ImageScrollView *page = [self dequeueRecycledPage];
            if (page == nil) {
                page = [[ImageScrollView alloc] init];
            }
            [self configurePage:page forIndex:fakeIndex];
            [pagingScrollView addSubview:page];
            [visiblePages addObject:page];
        }
    }
}
-(CGSize)contentSizeForPagingScrollView{
//出于上述相同的原因,我们必须使用分页滚动视图的边界来计算contentSize。
CGRect bounds=pagingScrollView.bounds;
double rightBounds=bounds.size.height;
//返回CGSizeMake(bounds.size.width*[self-imageCount],rightbunds);//没有无限循环
返回CGSizeMake(bounds.size.width*[self-imageCount]*100,rightbunds);//带无限循环
}
-(无效)瓷砖路面板
{
//计算哪些页面是可见的
CGRect visibleBounds=pagingScrollView.bounds;
int firstneedpageindex=floorf(CGRectGetMinX(visibleBounds)/CGRectGetWidth(visibleBounds));
int lastNeedPageIndex=floorf((CGRectGetMaxX(visibleBounds)-1)/CGRectGetWidth(visibleBounds));
firstNeedPageIndex=MAX(firstNeedPageIndex,0);
//LastNeedPageIndex=MIN(LastNeedPageIndex[self-imageCount]-1);//没有无限循环
LastNeedPageIndex=MIN(LastNeedPageIndex,[self-imageCount]*100-1);//具有无限循环
//回收不再可见的页面
用于(可视页面中的ImageScrollView*页面){
如果(page.indexlastneedpageindex){
[recycledPages添加对象:页面];
[page removeFromSuperview];
}
}
[visiblePages minusSet:recycledPages];
int fakeIndex=firstneedpageindex;
//添加缺少的页面
对于(int index=firstneedpageindex;index问题在于:

- (CGRect)frameForPageAtIndex:(NSUInteger)index {
    // We have to use our paging scroll view's bounds, not frame, to calculate the page placement. When the device is in
    // landscape orientation, the frame will still be in portrait because the pagingScrollView is the root view controller's
    // view, so its frame is in window coordinate space, which is never rotated. Its bounds, however, will be in landscape
    // because it has a rotation transform applied.
    CGRect bounds = pagingScrollView.bounds;
    CGRect pageFrame = bounds;
    pageFrame.size.width -= (2 * PADDING);
    pageFrame.origin.x = (bounds.size.width * index) + PADDING;
    // pageFrame.origin.y -= 44;
    return pageFrame;
}

框架将fakeIndex作为水平原点的乘数,因此新页面框架被放置在第一个页面框架的顶部。通过将fakeIndex和索引传递给[self-configurePage:page forIndex:fakeIndex],我可以将索引作为水平原点的乘数传递,然后新的页面框架就正确了。

看一看苹果的无限滚动条。还有一段来自WWDC 2011的关于UIScrollViews的简洁视频解释了这一点。谢谢。本例中的方法似乎有点不同,所以我坚持使用原始示例w我已经在这上面花了一些时间,最后终于弄明白了。