Drawing a line in a UIView subview - iphone

I have a UIView subclass. This subclass has multiple subviews. Is it possible to draw a line using core graphics inside a subview that is part of the uiview subclass?
For example, I have a SampleView class, which is a subclass of UIView. Inside this class's header file is the property for UIView *sampleSubView, which is a subview of SampleView. Is it possible to draw a line inside of sampleSubView from the SampleView class implementation?
Thanks for your help!
Josh

If you are asking how a UIView subclass can draw a line on itself, then see the Quartz demo sample code. Basically, you'll override the view's drawRect: method, get the current graphics context, then draw whatever you like onto it.
If you are asking how one view can draw a line on another view, perhaps you need to rethink your architecture.

Related

Using drawInRect on a view?

I have a drawInRect method which I wish to use on another UIView, a subview of this view.
Is there a way to do this?
If you are interested in duplicating the drawing, you could put your drawing in a separate function (drawFunct) of the top level view, and just call [self.superview drawFunct] from within the subviews drawInRect.
Calling the drawInRect is generally discouraged.

How to draw something on UIImageView in UIViewController Class

I know a method to draw on iPhone using UIView class and UIImage method. But can i draw inUIViewController class on UIImageView?
As UIImageView is a subclass of UIView, you should be able to use the exact same method to do what you want.
You might also add a custom view (inherit from UIView) above any other view and draw anything you want on that view.

iPhone: In a custom view, how to draw something on top of everything else in the view?

So I made a UIView subclass let's called it 'MyView' that simply draws a line inside the DrawRect method.
I then drag a UIImageView in interface builder and put it inside MyView. But now when I run the program it's obscuring the line that I'm drawing. I'm wondering, is there a way for me to draw the line on top of the image that I have dragged into my View?
No, you'll have to reorganise the view hierarchy. CoreGraphics uses a painters model so views behind will always be drawn over by views infront. To solve this you could use a container view that holds your image view and has a transparent view (line drawing view) that sits over the image view.
Another option is to draw the image using core graphics calls in your drawRect method and then draw the line over it.
One way is to have whatever adds MyView to add your UIImageView on top of MyView. It's not ideal (you probably have good reasons for keeping MyView and the UIImageView in a single class) but definitely solves your problem (I recently did this, only, I added it to the keyWindow =)
I would use a CAShapeLayer that has a path representing the line, and insert that layer ahead of the built-in view layer. Then it should draw on top.

UIView subclass creating its own UIButtons

I have subclassed UIView so I can draw some lines in it. In this UIView are a whole bunch of buttons which I decided to create as a method that drawRect calls after the lines are drawn. It works but sometimes only part of the button renders. If i remove the button creation and instead add the UIButton in the subclassed UIViews parent it works fine.
Is doing things like adding subviews in drawRect method a big no no or should I not do it all together in UIView?
yea that's a pretty big no no. drawRect is for adding stuff with CG, not views. It gets called repeatedly and unpredictably. Adding them in initWithFrame should be pretty safe though.

can i use CGAddLineToPoint and CGAddMoveToPoint in EAGLView

Error is like this:-
errror:-CGAddLineToPoint invalid context
I assume by EAGLView, you mean a custom UIView subclass that is backed by a CAEAGLLayer. Because such a view has its content drawn by the OpenGL layer, you can't do the same sort of Quartz drawing within it that you would in a UIView backed with a standard CALayer.
My recommendation is to move your custom Quartz drawing to another UIView subclass, and place that UIView as a subview of your CAEAGLLayer-hosting view.