I am currently building an objective c application in xcode that features a full screen horizontally scrolling collection view with cells that take up about 80% of the screen; similar to that of Instagram, Vine, etc. Initially in these cells are video thumbnails that are loaded from a backend source upon the loading of the view.
Since it would be terribly inefficient to load all of these videos at once, I am trying to find a way to only load one video at a time while the user scrolls through the collection view.
The way I am achieving this right now is by using the scrollViewDidEndDecelerating
method to calculate whichever cell is in the center of the screen after scrolling, and then beginning to load the video, as well as the scrollViewDidScroll
method to stop loading the video. This implementation is shown below:
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//calculate which cell is in the center
//load video in respective cell
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
//stop loading video
}
As you all can probably see, this generates a lot of ux problems:
- The video doesn't start loading until scrolling has stopped completely
- This approach doesn't account for drag gestures to navigate the Collection View
- Once a video has loaded, any drag or scroll stops loading/playing the video
- There are times where no video is loading, between scrolls
I am trying to turn this current approach into a system similar to the Instagram method of loading videos, which is as follows:
- A video begins to load as soon as it begins to show up on the screen while the user scrolls
- A video stops loading when a different video begins to appear on the screen while the user scrolls.
- One video is always being loaded, there is no downtime between loading one video and another
I understand that in order to achieve this functionality, these loading functions will need to be done on a background thread in order to allow for seamless scrolling which I can handle, I just need to know which methods I should be using instead of the ones i'm using now in order to achieve this functionality.