Last active
August 29, 2015 13:57
-
-
Save dokun1/9394219 to your computer and use it in GitHub Desktop.
Infinite UIScrollView w/Paging for UIImageViews - determining new page to load
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset { | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ | |
CGFloat xOffset = _scrollView.contentOffset.x; | |
CGFloat newStart = 0; | |
BOOL done = NO; | |
NSInteger newIndex = 1; | |
while (done == NO) { | |
if (newStart + 320 < xOffset) { | |
newStart += 320; | |
newIndex++; | |
}else{ | |
done = YES; | |
} | |
} | |
if (velocity.x < 0) { | |
newIndex--; | |
newIndex--; | |
}else{ | |
newIndex++; | |
} | |
if (newIndex >= [_metadata count] || newIndex < 0) | |
return; | |
id obj = _imageViewArray[newIndex]; | |
if ([obj isKindOfClass:[UIImageView class]]) | |
return; | |
AfterpartyPhotoInfo *currentInfo = _metadata[newIndex]; | |
CGRect currentFrame = CGRectMake(0, 0, currentInfo.size.width, currentInfo.size.height); | |
UIImageView *newImageView = [[UIImageView alloc] initWithFrame:currentFrame]; | |
[_imageViewArray replaceObjectAtIndex:newIndex withObject:newImageView]; | |
UIActivityIndicatorView *indicator = _downloadIndicators[newIndex]; | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
[_scrollView insertSubview:newImageView belowSubview:indicator]; | |
newImageView.frame = [self frameForImageViewAtIndex:newIndex]; | |
newImageView.center = [self centerForImageViewAtIndex:newIndex]; | |
[self getImageForIndex:newIndex]; | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment