Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

Draw a simple vertical bar in iphone

I wanted to draw a vertical single bar graph. I was trying to do it using DrawRect, but could not able to do so. Can nay one hlep me to knwo if this can be done easily by providing start and end point in view to change the color.

thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you have a custom view, it's just a matter of drawing the lines and rectangles that you want. For example:

- (void)drawRect:(CGRect)rect
{
    CGAffineTransform t = CGAffineTransformMakeScale(1, -1);
    self.transform = t;
    CGFloat baseline = 50;
    CGFloat inset = 40;
    CGFloat barWidth = 20;
    CGFloat barHeight = 80;

    CGRect r = CGRectMake(inset + barWidth, baseline, barWidth, barHeight);
    UIBezierPath *p = [UIBezierPath bezierPathWithRect:r];
    [[UIColor redColor] set];
    [p fill];
    p = [UIBezierPath bezierPath];

    [p moveToPoint:CGPointMake(inset, baseline)];
    [p addLineToPoint:CGPointMake(inset + 3 * barWidth, baseline)];
    [[UIColor blackColor] set];
    [p stroke];
}

produces this:

Sample bar graph

I've created a UIView subclass with the -drawRect: method above and created an instance of that view that's the size of the window. Note that I flipped the coordinate system using a transform -- you don't have to do that, but drawing with the origin at the lower left corner can be easier.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...