そこで、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) を調査し、何が問題なのかを理解するのに少し時間がかかる場合、それは素晴らしいことです.
ありがとうございました。