how to draw Arc in viewdidload method in iPhone

i try to draw arc in view i use this code.

- (void)viewDidLoad
{

    [super viewDidLoad];

    CGRect rect = CGRectMake(0,0,340,480);  
    UIView *ui = [[UIView alloc] initWithFrame:rect];
    [self.view addSubview:ui];
    CGContextRef context = UIGraphicsGetCurrentContext(); 

    CGContextAddArc(context, 50, 50, 20, 0, 30, 0); 
    //set the fill or stroke color
    CGContextSetRGBFillColor(context, 0.5, 0.5, 0.5, 1.0);
    CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 1.0);

    //fill or draw the path
    CGContextDrawPath(context, kCGPathStroke);
    CGContextDrawPath(context, kCGPathFill);
}

if possible to draw arc in viewdidload method kindly guide me.

Answers


It is not possible to draw an arc in view Did Load.

What you need to do is:-

1.)Add a file of type UIView(lets say ArcView.h and ArcView.m)in your project

2.)In ArcView.m file implement - (void)drawRect:(CGRect)rect method

3.)In this method you need to write your logic of drawing an arc or whatever you want to draw)

4.)Now the ViewController in which you have to show your arc lets say(ArcViewController is the file in which you want to show your arc)

5.)In ArcViewController.m file you need to do following steps:-

a.)#import "ArcView.h"

b.)Whenever you want to show your arc view do:-

ArcView *vw = [[ArcView alloc] initWithFrame:*your required frame where you want to show arc*];
vw.backgroundColor=[UIColor redColor];
[self.view addSubview:vw];
[vw release];

After that you will see your arc coming on your screen.


Your views should be drawing when requested (e.g. in drawRect:), rather than your view controllers drawing when loaded.

So what you may want to try is creating a UIView subclass and moving the drawing code to its drawRect:.

Very briefly:

@interface MONView : UIView
@end

@implementation MONView

- (void)drawRect:(CGRect)frame
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    <-- now draw here -->

}

@end

- (void)viewDidLoad
{
    [super viewDidLoad];

    CGRect rect = CGRectMake(0,0,340,480);
    UIView * ui = [[MONView alloc] initWithFrame:rect];
    [self.view addSubview:ui];
    [ui release];
}

Need Your Help

adding slietoogle effect to a hide and show div

jquery mouseover slidetoggle mouseout

Hello I'm using a javascript to hide or show a div.

Sucessive integers using C programming

c loops integer

Example input: 20 10 5 20 2 20 20 20 2 2 0