1

そこで、Apple の PhotoScroller サンプルを変更して、無限ループ機能を追加しようとしています。つまり、ユーザーが最後の画像までスクロールすると、最初の画像が次に表示されます。

そのために、contentSize の幅を 100 倍し (無限ループを偽造するため)、最後に必要なページ インデックスにも 100 倍し、偽のインデックス (インデックス 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)、ページは黒のままになり、画像は表示されなくなります。

ただし、コンソールを使用して [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の両方を[selfconfigurePage:page forIndex:fakeIndex]に渡すことで、水平方向の原点の乗数としてインデックスを渡すことができ、新しいページフレームが正しくなります。

于 2012-08-16T19:41:27.043 に答える