0

I'm trying to make my main ViewController with a custom view draw multiple custom UIView's to the main view, but somehow they are not drawing, I'm trying to draw dots.

My code is:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    MyCustomView *myView = (MyCustomView *)self.view;

    myView.xAxisLabel1 = @"customlabels 1";
    myView.xAxisLabel2 = @"customlabels 2";
    myView.xAxisLabel3 = @"customlabels 3";
    myView.xAxisLabel4 = @"customlabels 4";

    CustomDotView *newDot = [[CustomDotView alloc] initWithPointAtXCord:10 andYCord:10 withRadius:10 andColor:[UIColor redColor]];

    [self.view addSubview:newDot];


}

But this is not working, I wonder if the constructor of my CustomDotView is correct or I'm doing something incorrectly

This is my CustomDotView constructor

-(id)initWithPointAtXCord:(float)xCord andYCord:(float)yCord withRadius:(float)radius andColor:(UIColor *)color {

    self = [super init];
    self.color = color;
    self.xCordenate = xCord;
    self.yCordenate = yCord;
    self.radius = radius;

    return self;

}
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(context, 2.0);
    CGContextSetStrokeColorWithColor(context, color.CGColor);
    CGContextSetFillColorWithColor(context, color.CGColor);

    CGRect currentRect = CGRectMake(xCordenate, yCordenate, radius * 2 , radius * 2);

    NSLog(@"draw point?");

    CGContextAddEllipseInRect(context, currentRect);
    CGContextDrawPath(context, kCGPathFillStroke);
}

Any suggestions?

STW
  • 44,917
  • 17
  • 105
  • 161
perrohunter
  • 3,454
  • 8
  • 39
  • 55
  • 1
    You have to give your CustomDotView a frame. Right now, you're attempting to draw inside of a 0x0 rectangle. Make sure to call super `initWithFrame` as well. – SethHB Aug 30 '12 at 23:51
  • and I should create a CGRectMake with the dimentions of the dot right? Can I change this during runtime? – perrohunter Aug 30 '12 at 23:54
  • 1
    Yes, and you can even draw outside of the frame if you want. Just make sure to turn off `clipSubviews` on your custom view. Instead of making `currentRect` in your draw loop, simply make it in your Init and then it will be passed as the parameter in `drawRect:` – SethHB Aug 30 '12 at 23:57
  • I'm creating the rect and so far I'm seeing a black rect on the top of my screen but not the drawn dot with color, and I can't find the clipSubviews property anywhere CGRect rect = CGRectMake(10 , 10, 10, 10); self = [super initWithFrame:rect]; – perrohunter Aug 31 '12 at 00:04

1 Answers1

2

Ok, I went ahead and wrote this out in Xcode and it works great. Here's the simplest use case I could come up with; this is inside a blank UIViewController.

@implementation ViewController

- (void)viewDidLoad
{

    CustomDotView *newDot = [[CustomDotView alloc] initWithPointAtXCord:10 andYCord:10 withRadius:10 andColor:[UIColor redColor]];

    [self.view addSubview:newDot];

}

@end


@implementation CustomDotView

-(id)initWithPointAtXCord:(float)inputXCoord andYCord:(float)inputYCoord withRadius:(float)inputRadius andColor:(UIColor *)inputColor
{
    xCoord = inputXCoord;
    yCoord = inputYCoord;
    color = inputColor;
    radius = inputRadius;

    self = [super initWithFrame: CGRectMake(xCoord, yCoord, radius * 2, radius * 2)];

    self.backgroundColor = [UIColor clearColor];

    return self;

}
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(context, 1.0);
    CGContextSetStrokeColorWithColor(context, color.CGColor);
    CGContextSetFillColorWithColor(context, color.CGColor);


    CGContextAddEllipseInRect(context, rect);
    CGContextDrawPath(context, kCGPathFill);
}


@end

If you want to move the Dot around, just simply change it's frame.

SethHB
  • 743
  • 4
  • 12