核心图形drawRect指定坐标

时间:2011-10-21 06:38:43

标签: iphone ios cocoa-touch core-graphics

我刚刚进入iOS开发阶段。我正在修补核心图形,我正在试图弄清楚如何限制绘图的大小,并将其放置在屏幕上的特定坐标上。这是我的HomeViewController.m loadView和addCircle方法,以及我的Artist.m drawRect方法:

HomeViewController.m - >的loadView

- (void)loadView
{
    NSLog(@"HomeViewController.m : loadView");
    self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    UILabel *lbl;
    lbl = [[UILabel alloc] initWithFrame:CGRectMake(65, 50, 220, 70)];
    lbl.text = @"Home Screen";
    lbl.font = [UIFont boldSystemFontOfSize:30];
    [self.view addSubview:lbl];
    [lbl release];

    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoDark ];
    infoButton.frame = CGRectMake(10, 10, 300, 300);
    [self.view addSubview:infoButton];
    [infoButton addTarget:self action:@selector(loadInfo) forControlEvents:UIControlEventTouchUpInside];
    [infoButton release];

    [self addCircle];

    // self.view = [[[Artist alloc] init] autorelease];
}

HomeViewController.m - > addCircle

- (void)addCircle
{
    Artist *artist = [[[Artist alloc] init] autorelease];
    [self.view addSubview:artist];
    // [artist drawRect:CGRectMake(100, 100, 100, 100)];
}

Artist.m - >的drawRect

- (void)drawRect:(CGRect)rect
{
    NSLog(@"Artist.m : drawRect");
    // Drawing code
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGColorRef red = [[UIColor redColor] CGColor];

    CGContextFillRect(context, CGRectMake(130,200,120,120));
    CGContextSetFillColorWithColor(context,red);
    CGContextFillEllipseInRect(context, CGRectMake(130, 200, 120, 120));
}

基本上,我试图避免让绘图占据整个屏幕,而是限制在我制作它的大小并且坐在其他视图之上。

1 个答案:

答案 0 :(得分:0)

UIViewController的{​​{1}}方法调用viewDidLoad中,将自定义视图添加到现有视图中。然后使用[self.view addSubview:artistView]属性设置它的大小和位置。