Using drawInRect on a view? - iphone

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.

Related

Subclassed UIView shows black background if drawRect: is overridden

I have a custom class which I derived from the UIView class. What I want to accomplish is just drawing vertical black lines with a predefined distance between. When I override the drawRect method and write my code to draw vertical lines, the view just shows a complete black background. Then I noticed that even an empty overridden drawRect which only calls the superclass' drawRect results in a black background. When I comment out the drawRect, then the view shows through and becomes transparent as I expected.
This is the only code I am using in the drawRect which just calls the superclass method:
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
}
What can be reason of this behavior?
It's quite likely that there is some under-the-hood shenanigans going on with UIView, and that the base class doesn't actually implement drawRect, or some sneaky optimisation is happening.
The documentation does say not to call super if you are directly overriding UIView, and it also says you don't need to implement drawRect if all you do is set a background colour or you populate the content in other ways:
The default implementation of this method does nothing. Subclasses that use native drawing technologies (such as Core Graphics and UIKit) to draw their view’s content should override this method and implement their drawing code there. You do not need to override this method if your view sets its content in other ways. For example, you do not need to override this method if your view just displays a background color or if your view sets its content directly using the underlying layer object. Similarly, you should not override this method if your view uses OpenGL ES to do its drawing.
And:
If you subclass UIView directly, your implementation of this method does not need to call super. However, if you are subclassing a different view class, you should call super at some point in your implementation.
It's therefore quite likely that by calling super you are losing something. You don't show any of your actual drawing code so its difficult to see where you might be going wrong with that, but as a starting point, don't call super if you've directly overridden UIView.

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.

Drawing a line in a UIView subview

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.

How to Call drawRect programmatically in objective c

How to Call drawRect programmatically in objective c ?
I want to call drawrect method of a view in my UItabbarcontroller. How i can do this ? Thanks in advance..
Edit
I have to call when the view is not visible currently. It will be the first time i have to call that view
[myView.layer display];
Forces the view to draw itself straight away.
[myView setNeedsDisplay: YES];
Forces the view to redraw on the next event loop cycle.
However, if you need to call it even when it is not visible, I think there is something wrong with the design of your view class. You should only be doing drawing inside drawRect: not anything else. And if you are only doing drawing why do it when the view is not visible?
setNeedsDisplay
setNeedsDisplayInRect:
Swift:
yourView.setNeedsDisplay()
or
yourView.setNeedsDisplay(newRect)
view.layer.renderInContext(context: CGContext) can draw the content of your view into a CGContext, e.g. a bitmap.
view.snapshotViewAfterScreenUpdates(afterUpdates: Bool) gives you a snapshot of the view.

Drawing a CGLayer outside the method drawRect: faster?

I'm not really sure, what am I asking...
...but if make layer composites in separate methods, and draw them to the view also outside of the drawRect: method... ...then could my app performance get raised?
I'm actually subclassed a puzzlePiece:UIView class, where the puzzlepiece gets rendered, but the redraw invoked now with setNeedsDisplay since the drawing implementation takes place in the drawRect: method. The app lags.
Can I improve performance if I get rid of the so called drawRect: method?
The thing wants to get implemented: http://gotoandplay.freeblog.hu/
I think what you want to do here is to have a very single, simple UIView that covers the entire play area. Then put each tile in a CALayer, and attach all the CALayers to the UIView and move them around and rotate them. Then there should not need to be a -drawRect: at all. You'll do all your drawing in your layers, and you should get the best performance that way. CALayer is similar to UIView in principle, but much lighter weight (faster and simpler). CALayer is basically a view that can only draw; it can't handle touches or events. It's parent view has to do that for it. But that let's CALayer be much faster.