我尝试在 View 中绘制圆弧,我使用此代码。
- (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方法中绘制圆弧,请指导我。
最佳答案
您的 View 应该在请求时绘制(例如在 drawRect:
中),而不是您的 View Controller 在加载时绘制。
因此,您可能想要尝试的是创建一个 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];
}
关于iphone - iPhone中viewdidload方法如何绘制圆弧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8352395/