How to draw parts of a UIView as it becomes visible inside a UICollectionView?

On Android, I have representations of elements with a potentially large width in the horizontal scroll list. The view loads and draws fragments of “images” as parts of the view become visible in the list. This is an optimization to avoid drawing all the images at once, as this would be wasteful and slow. The processed content is essentially a sound form. Do the way you want to work, I can’t separate the pieces as separate presentation elements in the list. This strategy works great because of how the Android drawing architecture works.

Now I'm trying to use a similar pattern in iOS, but I have problems, and I'm not too sure how to come up with a solution.

In iOS, I use UICollectionViewone that draws large-width cells, and we need the same optimization of loading and drawing only visible fragments.


Solution 1:
Check which parts UIVieware visible and draw only those visible fragments. The problem with this solution is that when scrolling it UICollectionView UIViewdoes not draw the following cartridges that become visible. Here are examples of what I'm talking about.

UIViewloads the initial pieces.
They can be seen in different colors of the pieces: enter image description here


, , , . enter image description here


2:
UIView CATiledLayer. , , UICollectionView.

, , shouldDrawOnMainThread. , UIView , . , .


, CATiledLayer , , CALayer UIView?


1
preferredLayoutAttributesFittingAttributes, , . , - . , .:)

- (UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes

2
. 1 . a UIView , CATiledLayer. , , .:/

+4
1

, . , fadeDuration 0. CATiledLayer fadeDuration.

shouldDrawOnMainThread, , App Store:

@implementation MyTiledLayer

+(CFTimeInterval)fadeDuration {
    return 0.0;
}

+ (BOOL)shouldDrawOnMainThread {
    return YES;
}

@end

Swift:

class MyTiledLayer: CATiledLayer {
    override class func fadeDuration() -> CFTimeInterval {
        return 0.0
    }

    class func shouldDrawOnMainThread() -> Bool {
        return true;
    }
}

, ( ) . .

-, . scrollViewDidScroll: . visibleRect ,

CGRect theVisibleRect = CGRectIntersection(self.frame, self.superlayer.bounds);

Swift

let theVisibleRect = frame.intersection(superlayer.bounds)

.

+2

Source: https://habr.com/ru/post/1693421/


All Articles