How can I draw in the bounce area of ​​a UIScrollView?

I have a customized view that I include in a UIScrollView that scrolls horizontally. The view draws a background with lines extending horizontally and some different background colors. When I scroll to the far left so that the scroll view β€œbounces”, I see a gray background color. What I would like to do is draw additional background lines and colors in this area so that it looks like the view lasts forever, but I cannot figure out how to do it. I tried setting clipToBounds to NO for all views and drawing in the area outside the view, but this does not seem to work. How can I draw in this area?

+4
source share
2 answers

I found a solution to this, at least in my case. I just increased the scroll size with an extra field, and then slightly formatted the scroll.

CGFloat scrollViewHeight = 300; CGFloat preferredWidthOfContent = 500; CGFloat gutterMargin = self.bounds.size.width / 2; scrollView.frame = CGRectMake(-1 * gutterMargin, 0, self.bounds.size.width, scrollViewHeight) scrollView.contentSize = CGSizeMake(preferredWidthOfContent + 2 * gutterMargin, scrollViewHeight); contentView.frame = CGRectMake(0, 0, preferredWidthOfContent + 2 * gutterMargin, scrollViewHeight); 

The content presentation should know the width of the gutter field and then based on this draw the actual content in the right place. When the scrollView bounces, then the rest of the content is actually displayed.

This trick works because scrolling is viewed from the screen. If you want to use the same scroll list trick that didn't touch the edges of the screen, you just need to place a different view on top of the top of the scroll to hide the extra space.

+1
source

If you draw your lines in the drawRect: method, then there is no way to get them out of sight. The clipsToBounds property clipsToBounds affects subviews, so, theoretically, you can add a subview to your main view (displaying content) with a frame that extends to the right of your view and draw lines in this view.

Another option is to add a subview to the scroll view and place it to the right of your main view. Then draw lines in this subclause. Scrolling will still bounce, since it doesn't care if it has partially visible views, it only looks at the contentSize property to decide when to stop scrolling.

If none of the options fits, describe in more detail which lines you draw and what kind you would like to achieve.

0
source

All Articles