How to use drawRect with a subclass of UIScrollView? - iphone

I'm trying to implement an owner-drawn view that is a subclass of UIScrollView. Basically, I want to custom-draw the contents of the view, and then have the stuff I've drawn be scrollable.
I've overridden drawRect in my view and I'm able to draw my contents, scaled to the size of the UIScrollView's contentSize property (so some of my custom drawing is not visible, as I intended).
The problem is that the content then never moves. I can drag my finger up and down, and this makes the UIScrollView's scrollbars appear, but my custom-drawn content never moves or changes - I still always only see the top half.
How can I custom-draw content for this UIScrollView so that what I've drawn is scrollable?

Are you calling [super drawRect]; at the end of your own drawRect method?
Edit
I misread the question. You'll need to create your own UIView subclass and put your overridden drawRect in that. Then, add that view as a child of the UIScrollView.

Related

Overlaying a UIScrollview without cutting off touch events to the scrollview

I have a transparent overlay that I'd like to put over a UIScrollview. I'm adding it as an Imageview sibling view to the scrollview so that it remains stationary while the scrollview subviews move freely underneath. The problem is that views pass their events to the superview, not the siblings. IS there a way to pass events from this overlay to the scrollview? Or can anyone think of a better way to achieve the same effect? Thanks!
This should Just Work, as long as the UIImageView has its userInteractionEnabled property set to NO: the superview sends -hitTests:withEvent: to its subviews in order, and the UIImageView should return nil, whereas the UIScrollView should return itself (because it has gesture recognizers).
If it's not working for you, the chances are that your view layout is not what you think it is. UIView has a useful method called -recursiveDescription which you should call on the superview and NSLog the result.

Any way to draw Core Graphics in the contentView of a UIScrollView

I was wondering if it was possible to draw any core graphics in the content view of a scroll view because at the moment, in the drawRect section of the UIScrollView, it just draws on the static view and when I scroll the content view, the graphics stay still... (which means I'm not drawing in the right view)
Do I have to create another UIView subclass and draw there and add it to the UIScrollView, while handling the zooming and scrolling from the UIScrollView
or - is there a way I can keep it all in the one subclass of UIScrollView!
(I hope that makes sense!) thanks!
Typically, you don't draw in a UIScrollView directly. UIScrollView is just a container view which scrolls one content view or several tile views. You should add the content view(s) with addSubview: and position them as needed. The scroll view will handle scrolling and zooming.

Second UIScrollView responding when using a UIScrollView

This is more of a check as I believe this is right but its a lot of work if I'm wrong.
I want to basically achieve fixed positioning with a scrollView. I want to have a list along the top that is always visible, scrolls horizontal only and then a scrollview beneath that to move around the information which scrolls both vertically and horizontally.
I figure I need to subclass UIScrollView and overwrite touchesBegan, touchesMoved and touchesEnded to send the touch to both UIScrollViews.
Is this right or off track?
Cheers
Overriding the touch events on a scroll view is probably not what you want to do. Instead you can simply use a single scroll view, and then in the parent view's -layoutSubviews or in the scroll view's delegate methods you can move the list so it's always at the same vertical position (use the scroll view's contentOffset property to determine where that should be). Both the delegate method and -layoutSubviews is called before the drawing actually occurs after the scroll view scrolls, so by always repositioning your view where you want it to be, it will appear to remain fixed to the user.

UIScrollView and CALayers

I have a custom UIView that is composed entirely of CALayers.
In the awakeFromNib method it creates and sets all the CALayers into their appropriate positions (CAGradientLayer, several CATextLayers, and a few custom CALayer subclasses). The custom UIView does not override the drawRect: method because there's no drawing done directly into the view (all of the drawing is done in the sublayers).
So I took this view and embedded it in a UIScrollView. The problem? No scroll bars appear and the view does not scroll. The view is clearly larger than the bounds of the scroll view, and instead of allowing me to scroll, it just cuts off at the scroll view bounds.
What could be wrong here?
You have to set the scrollView's contentSize.

Drawing a shadow below a UIScrollView (that scrolls a UIView)

My setup is a UIScrollView in the center of the screen (on the iPhone - like 300x400 positioned in the center) that contains a UIView of the same width, so it scrolls it vertically. In this UIView i draw custom subviews with labels etc (it's a scoreboard with various colors).
What i'd like to have is some shadow below my UIScrollView, so that the whole scrolling scoreboard floats over my background.
I have found this nice post
How do I draw a shadow under a UIView?
I use this code in my ScrollView subclass but it doesn't work for me. Maybe because I don't draw the actual shapes in the ScrollView's drawRect: (since they are drawn on the UIView).
Also I guess that in order to have the View scroll in the ScrollView and the shadow of the ScrollView outside the scrolling area, I guess I should extend the "bounds" of the ScrollView, right?
It's not quite clear to me what you're asking but, if you want the scrollView contents to scroll over a static image you simply need to add a UIView (or more likely a UIImageView) to your superview and then add your UIScrollView to that. If you set he background colour of the UIScrollView to be celarColor, the background image will show through - so you have a view heirarchy like:
UIWindow
UIView <----- your background here
UIScrollView
Scrolling subviews <----- high score table here
If you draw your highscore table in the scrolling subviews using CoreGraphics, the answer in the question you linked to will also work.
How about explicitly filling the entire self.bounds rectangle in your scroll view subclass' drawRect: method before calling super?
Another idea is to put the scroll view inside of another view which does the shadow drawing.