1
votes

Seems like a very simple problem! But I'm having great difficulty.

My ideas and attempts so far:

  1. scrollView.backgroundColor = [UIColor colorWithPatternImage:myImage]]
    • Doesn't scroll with contents
  2. Create UIView the size of contentSize (actually, larger due to elastic bounces on scroll view), and use pattern as above on this sub view of UIScrollView.
  3. Similar to (2), exept I only create a UIView the size of the maximum number of repetitions of the background image that will be seen, and cleverly move the view to wherever is needed to show the correct background repetitions.
    • Causes this background view to shift left and right unexpectedly when animating the scroll.
  4. Create UIView subclass and override drawRect. Use various Core Graphics techniques to draw content by hand.
    • Not animatable. Implementing my own contentOffset property isn't a standard Core Animation property.
    • Overriding drawRect on UIScrollView doesn't respect the content offset, and doesn't get called multiple times as it scrolls/animates. The rect parameter is always simply the bounds of the UIScrollView.
  5. As with (4), except I set the bounds.origin of my UIView subclass in my setContentOffset implementation, since it's an animatable property.
    • drawRect doesn't seem to get called every frame.
  6. Use CATiledLayer, as suggested in this answer: Large UIScrollView with background pattern fails. Implementation details here: http://www.cimgf.com/2011/03/01/subduing-catiledlayer/.
    • I really don't want the ugliness of seeing tiles asynchronously being drawn as user scrolls. It's just a simple background pattern!

This seems like the simplest thing! Why is it so hard!?

1

1 Answers

1
votes

Maybe the sample code:ScollViewSuit->3_Tiling can help you. You can search it in the official docset.

This works like CATiledLayer but only use UIKit, the tile was loaded on the main thread.

And I really don't think this is a good solution.