basic question, but I'm unsure. Not looking for code as an answer.
I want to draw 4 short lines 1px lines on a view. What is the best way to approach this task? Options:-
Load an image of the line, then create 4 UIImageViews with it.
Create my own subclass of a UIView that draws a line in the draw rect method.
Draw elsewhere on another view, another UIImageView that has an UIImage inside it (is this possible?)
Another way?
Thanks
Ross
Simplest way is to create a UIView subclass and perform your drawing in drawRect:. See the CoreGraphics guide for details on how to draw a line.
Quick and dirty way to achieve this is to just create a UIView and set it's height (or width, depending on its orientation) to 1px, and then set a background colour and slap it onto your view as a subview.
Related
I hope that makes sense. I'll try to explain it.
I have a UIImageView on screen, and am wondering how I can take the area after "drawing" on it with a finger, and remove that section from the UImage, or, create a separate UIImage from the selection.
I'm not looking for code (unless you have it =] ), just an idea of how to go about doing it. If you have tips, I'd be very grateful, thanks.
If I understand your question,
I think I would add a transparent UIView as a subview over the top of the UIImageView. And draw on that. Then you can remove/hide the subview when your done.
you need to create a UIGestureRecognizer with target and a action like -imageIsPressed, in this -imagePressed method you can call something to make the image disappear. I would suggest placing the UIImage into a UIImageView and calling imageview.hidden = YES; to hide the image, and set it back to "NO" once its not held by the finger.
You'd need to implement something that captures the area the user 'selected' (maybe be creating a CGPath. Then you create a CALayer of the size of the imageView. In it you create, draw and fill the captured path with some arbitrary color while leaving the rest transparent. Finally you apply your generated CALayer as a mask to the UIImageView:
imageView.layer.mask = maskLayer;
Hope that gets you started.
For more info on how to draw that custom CALayer pls refer to Quartz Programming Guide
So basically you want to implement freehand erasing of an image? You will need to use core graphics and the various CGContext methods (with blend mode set to clear) to achieve this. There are two approaches, but both start with drawing your image as the first part of drawRect, and then
1) Store your strokes in an array, and stroke all of them over top of the image.
2) Stroke one stroke over the image and then store the resulting image into a UIImage. Use this UIImage as the next image that you draw in drawRect. This one is difficult for undo/redo functionality.
I recently implemented this myself and made the source available here. Basically I used the same methods described here with this change when setting up the graphics context:
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeClear);
I am drawing some text and images to a view in the "DrawRect" method.
I wish to understand :
does my drawings are separated objects?
i know how to animate UIViews but how do i reach one of them so i can animate his properties ?
thanks
shani
O.k
if any one wants to know the answer. you can not animate drawn text or objects unless you draw it to CALayer.
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.
On my iPhone app, I would need to draw a kind of full gauge with a dynamic indicator.
I was thinking of using a gauge image (a .png, that I would draw in an external tool) without any indicator, and then draw the dynamic indicator on top of the image.
I was thinking of using a UIView with this image in background and then then use CoreGraphics (that I've never tried) to draw the indicator.
Do you think it's the easiest way to do this ?
thanks a lot,
Luc
I think you can't have a background image in the same view.
1°) You draw the image each time drawRect() is called
2°) You have two UIView, one who have the background image, and the other one which draw your gauge ;-)
Good Luck !
(I think the easiest method is the second one which don't re-draw the background each time :-))
Luc,
Your approach should work fine. Create a custom UIView and override -drawRect: to first draw the background PNG of the gauge, then draw the indicators.
- (void) drawRect: (CGRect) rect {
\\ draw the gauge PNG into the view bounds
...;
\\ Now use Quartz to draw the indicators
...;
}
I've created a NIB file with some button controls on it, and as a background it has an ImageView which contains a PNG file loaded from my project.
What I want to do is to draw on top of the ImageView - imagine that my UI is a clockface and I want to draw the hands of the clock on top of the background image.
Is it the correct approach to try to subclass the UIImageView and use its DrawRect as when I use the DrawRect of the default view I don't see anything on the screen?
I'm a little lost as I'm finding this particular bit of the documentation hard to follow.
Thanks.
Create a new custom UIView (e.g. named HandsView) that sits on top of your background view (by adding it as a subview of the UIImageView). In the custom view, you can use the drawRect method. Make sure, you clear the context to transparent, so that the background image can be seen below the HandsView.
If you just want to draw turning hands, you can also try to use two fixed UIImageViews with the images of the hands and use the transform property to apply a rotation.