我使用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__);
}
}
发布于 2015-02-27 05:04:42
最后,在上面的code.Here中找到解决方案并创建无限滚动视图,我们使用相同的代码为视图设置框架。
代码:
//在这里,我在数组的第一个索引和最后一个索引中添加了两个虚拟数据,在第一个位置添加了最后一个对象,最后添加了arrData.When数据集的第一个对象,那时我们创建了第一个对象和最后一个对象的两个虚拟数据。
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];
}
}
}
发布于 2015-02-22 22:41:38
下面是如何生成无限滚动视图的方法。这个应用程序使用可可豆,你必须从终端下载。Github有许多令人敬畏的开源框架,可以在您完成任务时跟上您的想法。https://github.com/pronebird/UIScrollView-InfiniteScroll
https://stackoverflow.com/questions/28667902
复制