因此,我正在修改 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),并找出问题所在,那就太好了.
谢谢你。