So, you have a UIView inside a UIScrollView , but you want your UIView have very large borders (i.e. it matches the size of your UIScrollView contentSize ). But you do not want to draw the entire UIView every time it should be displayed, and you cannot completely place all its contents in memory.
Make your UIView using CAScrollLayer as follows:
// MyCustomUIView.m + (Class) layerClass { return [CAScrollLayer class]; }
Add a method to update the scroll position when the user scrolls the UIScrollView containing your UIView :
Make sure that when drawing the UIView you only draw the parts contained in the CGRect provided to you:
- (void)drawRect:(CGRect)rect {
Now, in your UIScrollViewDelegate , you will need to notify your supported CAScrollLayer when updating the parent UIScrollView :
You can also use CATiledLayer , which is easier because you do not need to track the scroll position, instead, your drawRect method will be called with each tile as needed. However, this will make your view slowly fade. Perhaps it would be advisable if you intend to cache parts of your view and are not against slow updates.
source share