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__);
}
}
Here is how you make an infinite scroll view. This app uses cocoa pods which you must download from the terminal. Github has a lot of awesome open source frameworks to keep up with your ideas as you go along. https://github.com/pronebird/UIScrollView-InfiniteScroll
Finally found solution and create infinite scrollview in above code.Here we have use same above code for frame setting for view .
Code:
//Here i add two dummy data at first and last index of array, In first position last object i have added and at last object i have added first object of arrData.When Data set at that time we create two dummy data of first object and last object.
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];
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With