iphone - iPhone中viewdidload方法如何绘制圆弧

标签 iphone xcode

我尝试在 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/

相关文章:

ios - 无法生成 App 特定的共享密码

ios - UIBubbleTableView 从 NSBubbleData 获取 didSelectRowAtIndexPath?

iphone - 音频文件无法在 iPhone 5 中播放

iphone - 如何使用 Cocos2d 来创建一款这样的游戏

ios - 需要删除一个 subview

xcode - 一个 TableView 在多个 ViewController 中实现

objective-c - 使用 Xcode 4 编译 libogg

ios - iOS7 和 Xcode5 中的 MRC

iphone - UILabel 中实际上可以动画化的是什么?

iphone - 困惑的 Interface Builder XIB 中的连接数据源和委托(delegate)