I am making a custom View where I am plotting a curve. Now I want the background of that curve to be like a graph paper.
shall I use a vertical and horizontal lines Or draw a series of rectangles Or use background image?
currently I am using vertical and horizontal lines but the problem is even when I am setting the thickness of the line to be 1 pixel, It still seems to be thicker and If I reduce the thickness to say 0.5 then the color becomes lighter than what I have set it to.
For this kind of thing, if you don't expect to have to make many, many dynamic changes to the background image, you could just use a carefully-crafted .png. You can even make the thing a single square and then use
view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"template"]];
Although, if you're doing plot work, then you may want to draw the lines manually as you are. The answer to your question then is to make the stroke width 1.0 but to draw the lines at the halves: so to draw a vertical line down the 100th x pixel column, move the cursor to (99.5, 0.0) and stroke to (99.5, 480.0). CoreGraphics drawing routines draw your stroke centered on the theoretical line you create, and will antialias to physical pixels as necessary.
Related
how can i add an X looking red stroke to a UIImageView.
i would like to add 2 diagonal red lines to a UIImageView, is there a way to do it programmatically using layers or masks? (not in drawRect)
Use a CAShapeLayer with your X shape as its path. Depending on how you've drawn the path, you may want to set a nil fill colour (since a path just made of two crossed lines should not be filled).
Add the shape layer as a sublayer of your image view.
I am drawing a line using CGContext and color has transparency of 0.7 , Now i want to draw another line with another color with same transparency on that previous line . But i get the second line color as a solid color without any transparency in the part where this both two lines intersect. For first line i am using blend effect clear to draw a transparent line and for the second line i am using blend effect color. Please tell me how to draw these two lines separately so that the second line drawn can have its own separate color .
The default blending mode (kCGBlendModeNormal) should provide the behavior you want in both cases.
I'm wondering how I should go about drawing a uibezierpath where the stroke width peaks at the center of the arc. Here's an example of what I mean:
Either I have to go through each point when drawing, and set the stroke width accordingly, or there's an easier way. Can anyone point me in the right direction?
Thanks
You can just draw the two outer paths with no stroke, join them, and then fill in the space between them.
Another way to try this if you're interested:
I ended up getting this to work by creating a loop to draw a couple hundred line segments, and changing the line width accordingly during the draw loop.
To adjust the line width I used the following function: MAX_WIDTH * sinf(M_PI * (i/NUMBER_OF_SEGMENTS)
Looks great and no performance issues as far as I can tell. Worked out particularly well because I already had a list of the points to use on the curve. For other cases I'm guessing it would be better to use sosborn's method.
I am trying to use a UIImage with stretchableImageWithLeftCapWidth to set the image in my UIImageView but am encountering a strange scaling bug. Basically picture my image as an oval that is 31 pixels wide. The left and right 15 pixels are the caps and the middle single pixel is the scaled portion.
This works fine if I set the left cap to 15. However, if I set it to, say, 4. I would expect to get a 'center' portion that is a bit curved as it spans the center while the ends are a little pinched.
What I get is the left cap seemingly correct, followed by a long middle portion that is as if I scaled the single pixel at pixel 5, then a portion at the right of the image where it expands and closes over a width about twice the width of the original image. The resulting image is like a thermometer bulb.
Has anyone seen odd behavior like this and might know what's going on?
Your observation is correct, Joey. StretchableImageWithLeftCapWidth does NOT expand the whole center of the image as you would expect. It only expands the pixel column just right of the left cap and the pixel row just below the top cap!
Use UIView's contentStretch property instead, and your problem will be solved. Another advantage to this is that contentStretch can also shrink a graphic properly, whereas stretchableImageWithLeftCapWidth only works when making the graphic larger.
Not sure if I got you right, but LeftCapWidth etc is made for rounded corners, with everything in the rectangle within the rounding radius is stretched to fit the space between the 'caps' on the destination button or such.
So if your oval is taller or wider than 4 x 2 = 8, whatever is in the middle rectangle will be stretched. And yours is, so it would at least look at bit ugly! But if it's not even symmetrical, something has affected the stretch. Maybe something to do with origin or frame, or when it's set, or maybe it's set twice, or you have two different stretched images on top of each other giving the thermometer look.
I once created two identical buttons in the same place, using the same retained object - of course throwing away the previous button. Then I wondered why the heck the button didn't disappear when I set alpha to 0... But it did, it's just that there was a 'dead' identical button beneath it :)
Is it possible to adjust the alignment, from the default centered, of a stroke when using kCGPathFillStroke as the drawing mode?
For example, when drawing a closed path using:
CGContextDrawPath(context, kCGPathFillStroke);
The stroke lies 50% outside/50% inside the drawn path. The stroke color has a non-opaque alpha so the net effect is undesirable. I want the stroke to lie either all inside or outside.
The only potential way I see of doing this is to rebuild the path, contracted by 50% of the stroke width.
Your guess is correct. Adjust the path's position.