我使用this code在UIScrollView
中制作重用视图,效果很好。知道如何在UIScrollView
中使用重用子视图使这段代码无限滚动吗?
代码
#pragma mark
#pragma mark - SCROLL VIEW DELEGATE METHOD
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[self setNeedsLayout];
int currentPosition = floor(scrollView.contentOffset.x);
currentPage = MAX(0,floor(currentPosition / scrollView.frame.size.width));
if(currentPage != refPage)
{
refPage = currentPage;
if ([arrQLst count] > 0)
{
[self replaceHiddenView];
}
}
}
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
if ([arrQLst count] > 0)
{
[self replaceHiddenView];
}
}
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-(void)replaceHiddenView
{
const double pageWidth =self.frame.size.width;
NSInteger currentIndex;
currentIndex = (([self getScrollView].contentOffset.x - pageWidth) / pageWidth) + 1;
RandomView *currentView = nil;
RandomView *previousView = nil;
RandomView *nextView = nil;
if (CGRectContainsPoint(objRandom1.frame, [self getScrollView].contentOffset))
{
currentView = objRandom1;
previousView = objRandom2;
nextView = objRandom3;
}
else if (CGRectContainsPoint(objRandom2.frame, [self getScrollView].contentOffset))
{
currentView = objRandom2;
previousView = objRandom1;
nextView = objRandom3;
}
else
{
currentView = objRandom3;
previousView = objRandom1;
nextView = objRandom2;
}
currentView.frame = CGRectMake(self.frame.size.width * currentIndex, 0.0, self.frame.size.width, self.frame.size.height);
[currentView setQuestionData:[arrQLst objectAtIndex:currentIndex]];
// Now move the other ones around
// and set them ready for the next scroll
if (currentIndex < [arrQLst count] - 1)
{
nextView.frame = CGRectMake(self.frame.size.width * (currentIndex + 1), 0.0, self.frame.size.width, self.frame.size.height);
[nextView setQuestionData:[arrQLst objectAtIndex:(currentIndex + 1)]];
}
if (currentIndex >= 1)
{
previousView.frame = CGRectMake(self.frame.size.width * (currentIndex - 1), 0.0, self.frame.size.width, self.frame.size.height);
[previousView setQuestionData:[arrQLst objectAtIndex:(currentIndex - 1)]];
}
if([arrQLst count] > 0)
[self checkForRequestForLoadMore:currentIndex];
}
-(void)displayViewAtIndex:(NSInteger)index
{
@try
{
NSInteger count = [arrQLst count];
if (index >= 0 && index < count)
{
Ques *objQ = [arrQLst objectAtIndex:index];
objRandom1.frame = CGRectMake(self.frame.size.width * index, 0.0,self.frame.size.width,self.frame.size.height);
[objRandom1 setQuestionData:objQ];
[[self getScrollView] scrollRectToVisible:CGRectMake(self.frame.size.width * index, 0.0, self.frame.size.width, self.frame.size.height) animated:NO];
if (index < (count - 1))
{
objQ = [arrQLst objectAtIndex:(index + 1)];
objRandom2.frame = CGRectMake(self.frame.size.width * (index + 1), 0.0, self.frame.size.width, self.frame.size.height);
[objRandom2 setQuestionData:objQ];
}
if (index > 0)
{
objQ = [arrQLst objectAtIndex:(index - 1)];
objRandom3.frame = CGRectMake(self.frame.size.width * (index - 1), 0.0, self.frame.size.width, self.frame.size.height);
[objRandom3 setQuestionData:objQ];
}
}
}
@catch (NSException *exception)
{
NSLog(@"displayViewAtIndex Exception %s",__PRETTY_FUNCTION__);
}
}
答案 0 :(得分:3)
以下是制作无限滚动视图的方法。这个应用程序使用可可豆荚,你必须从终端下载。 Github有很多很棒的开源框架,可以随时跟上你的想法。 https://github.com/pronebird/UIScrollView-InfiniteScroll
答案 1 :(得分:2)
最后在上面的代码中找到了解决方案并创建了无限的scrollview。这里我们使用相同的代码进行视图的框架设置。
代码:
//这里我在数组的第一个和最后一个索引处添加两个虚拟数据 第一个位置我添加的最后一个对象,我最后的对象 添加了arrData的第一个对象。当我们创建数据集时 第一个对象和最后一个对象的两个虚拟数据。
if ([arrQLst count] > 1)
{
[arrQLst insertObject:[arrQLst lastObject] atIndex:0];
[arrQLst addObject:[arrQLst objectAtIndex:1]];
}
//end
[self setContentSizeOfScrollView];
- == - == - == - == - == - == - == - == - == - == - == - == - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = -
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[self setNeedsLayout];
int currentPosition = floor(scrollView.contentOffset.x);
currentPage = MAX(0,floor(currentPosition / scrollView.frame.size.width));
if(currentPage != refPage)
{
refPage = currentPage;
if ([arrQLst count] > 0)
{
[self replaceHiddenView];
}
}
if (scrollView.contentOffset.x <= 0.0)
{
if ([arrQLst count] > 1)
{
[self displayViewAtIndex:([arrQLst count]-2)];
}
}
else if (scrollView.contentOffset.x > (scrollView.frame.size.width * ([arrQLst count] - 1)))
{
if ([arrQLst count] > 1)
{
[self displayViewAtIndex:1];
}
}
}