I use this code to make reuse view in UIScrollView
, and it works well. Any idea how to make this code to infinite scrolling with reuse subview in UIScrollView
?
Code
#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__);
}
}
Aucun commentaire:
Enregistrer un commentaire