简体   繁体   中英

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.

Your views should be drawing when requested (eg 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];
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM