0

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:

  1. The video doesn't start loading until scrolling has stopped completely
  2. This approach doesn't account for drag gestures to navigate the Collection View
  3. Once a video has loaded, any drag or scroll stops loading/playing the video
  4. 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:

  1. A video begins to load as soon as it begins to show up on the screen while the user scrolls
  2. A video stops loading when a different video begins to appear on the screen while the user scrolls.
  3. 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.

Roger99
  • 981
  • 2
  • 11
  • 42
  • Rather than trying to infer cell visibility based on scrolling you might want to check out `UICollectionViewDelegate`'s `willDisplay:` and `didEndDisplaying:` methods, if you haven't already. As the names suggest, these will tell you directly when you might want to kick off loading based on a cell being displayed. – davidf2281 Jan 28 '17 at 13:59

1 Answers1

0
  1. You should make it such that the video uses "lazy loading" to load its contents/thumbnail.

  2. To make it play when cell is at center, use scrollViewDidScroll to detect if the cell is completely showing by method indicated at this answer

Community
  • 1
  • 1
GeneCode
  • 7,545
  • 8
  • 50
  • 85
  • I know how to make a video play when it is at the center and I know how to load its contents, I am trying to change the functionality to what I described in the second list. – Roger99 Jan 27 '17 at 02:40
  • Maybe I misunderstood, what you want in second list is simply lazyloading? – GeneCode Jan 27 '17 at 02:51