1

因此,我正在修改 Apple 的 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];
        }
    }
}

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

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

如果有人可以花一点时间调查原始示例代码(http://developer.apple.com/library/ios/#samplecode/PhotoScroller/Introduction/Intro.html),并找出问题所在,那就太好了.

谢谢你。

4

1 回答 1

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],我可以将索引作为水平原点的乘数传递,这样新的页面框架就会是正确的。

于 2012-08-16T19:41:27.043 回答