PhotoScroller样本中的无限循环

时间:2012-08-16 19:17:05

标签: objective-c ios photo-gallery

所以我正在修改Apple的PhotoScroller示例,并且我正在尝试添加无限循环功能,即:当用户滚动到最后一张图片时,第一张图片会显示下一张图片。

为此,我将contentSize宽度乘以100(伪造无限循环),将最后需要的页面索引乘以100,并使用假索引(index modulo 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];
        }
    }
}

因此只要index == fakeIndex,图片就能正常显示,但是一旦循环超过该点,例如:index = 5,fakeIndex = 1,页面将保持黑色并且图像不再显示。< / p>

但是,我可以看到使用控制台调用[self configurePage:page forIndex:fakeIndex]时,会获取正确的图像名称。

如果有人花一点时间调查原始示例代码(http://developer.apple.com/library/ios/#samplecode/PhotoScroller/Introduction/Intro.html),并弄清楚出了什么问题,那么会很棒的。

谢谢。

1 个答案:

答案 0 :(得分:0)

问题在于:

- (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和index传递给[self configurePage:page forIndex:fakeIndex],我可以将索引作为水平原点的乘数传递,然后新的页面框架将是正确的。