如何在iPhone中用viewdidload方法绘制圆弧
我尝试在视图中绘制圆弧,我使用此代码如何在iPhone中用viewdidload方法绘制圆弧,iphone,xcode,Iphone,Xcode,我尝试在视图中绘制圆弧,我使用此代码 - (void)viewDidLoad { [super viewDidLoad]; CGRect rect = CGRectMake(0,0,340,480); UIView *ui = [[UIView alloc] initWithFrame:rect]; [self.view addSubview:ui]; CGContextRef context = UIGraphicsGetCurrentConte
- (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);
}
如果可能在viewdidload方法中绘制圆弧,请指导我。您的视图应在请求时绘制(例如在
drawRect:
中),而不是在加载时绘制视图控制器
因此,您可能需要尝试创建UIView子类,并将绘图代码移动到其drawRect:
非常简单地说:
@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];
}
@interface MONView:UIView
@结束
@实现视图
-(void)drawRect:(CGRect)框架
{
CGContextRef context=UIGraphicsGetCurrentContext();
}
@结束
-(无效)viewDidLoad
{
[超级视图下载];
CGRect rect=CGRectMake(0,0340480);
UIView*ui=[[MONView alloc]initWithFrame:rect];
[self.view addSubview:ui];
[用户界面发布];
}
无法在负载视图中绘制圆弧
您需要做的是:-
1.)在项目中添加类型为UIView
(例如ArcView.h
和ArcView.m
)的文件
2.)在ArcView.m
文件中实现-(void)drawRect:(CGRect)rect
方法
3.)在这种方法中,您需要编写绘制圆弧或任何想要绘制的对象的逻辑)
4.)现在,您必须在其中显示弧的ViewController
让我们说(ArcViewController
是您要在其中显示弧的文件)
5.)在ArcViewController.m
文件中,需要执行以下步骤:-
a、 )#导入“ArcView.h”
b、 )无论何时要显示弧形视图,请执行以下操作:-
ArcView *vw = [[ArcView alloc] initWithFrame:*your required frame where you want to show arc*];
vw.backgroundColor=[UIColor redColor];
[self.view addSubview:vw];
[vw release];
之后,您将在屏幕上看到您的arc