Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setNeedsDisplay not firing DrawRect

I have a viewController which is obviously a subclass of UIViewController called MapViewController. In this viewController I use GPS to get the location of the user.

I also have a view called DrawCircle. This view is a subclass of UIView.

Using drawCircle I would like to be able to at any time draw on my MapViewController. But I am not sure I am understanding the concept of doing so. I know my drawing code is working, I have used it before. But I don't know how to draw onto MapViewController using DrawCircle.

From what it seems to my whenever I call [myCustomView setNeedsDisplay], it is not calling the DrawRect method in my view.

Here is some code: MapViewController.h

#import "DrawCircle.h"


@interface MapViewController: UIViewController <CLLocationManagerDelegate>{
    DrawCircle *circleView;
}

@property (nonatomic, retain) DrawCircle *circleView;
@end

MapViewController.m

#import "DrawCircle.h"

@interface MapViewController ()
@end

@implementation MapViewController
@synthesize circleView;

- (void) viewDidLoad
{
    circleView = [[DrawCircle alloc] init];
    [self setNeedsDisplay];

    [super viewDidLoad];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end

DrawCircle.m

#import "DrawCircle.h"

@interface DrawCircle() 
@end

@implementation DrawCircle

-(id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if(self) {
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{

    CGContextRef ctx = UIGraphicsGetCurrentContext();


      CGPoint point = CGPointMake(40, 137);
      CGContextAddEllipseInRect(ctx, CGRectMake(point.x, point.y, 10, 10));
    }

    CGContextSetFillColor(ctx, CGColorGetComponents([[UIColor redColor] CGColor]));
    CGContextFillPath(ctx);
}

Also if this offers any help into my thought process, here is my StoryBoard scene. StoryBoard Scene

Where the viewcontrollers custom class is MapViewController and the views custom class is DrawCircle.

**EDIT:**I would also like to mention that, in my DrawCircle.m, I have methods that I am calling from MapViewController.m and are working.

Also. Initially, the DrawRect method is being called but I am not able to manually call using setNeedsUpdate. When debugging, it is not even entering the DrawRect method.

like image 771
Johnrad Avatar asked May 01 '13 03:05

Johnrad


1 Answers

You're creating your DrawCircle, but you're never adding it to the view, e.g.

[self.view addSubview:circleView];

Therefore, it's falling out of scope and (if using ARC) getting released on you. You also don't appear to be setting its frame, such as:

circleView = [[DrawCircle alloc] initWithFrame:self.view.bounds];

Alternatively, you could add the view in interface builder (a standard UIView, but then specify your custom class right in IB).

Also, note, you generally don't even to call setNeedsDisplay. The adding it to the view hierarchy will call this for you. You only need to call this if you need to update the view based upon some custom property.


Personally, I'd be inclined to define drawRect like so:

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

    // I'd just use `rect` and I can then use the `frame` to coordinate location and size of the circle

    CGContextAddEllipseInRect(ctx, rect);

    // perhaps slightly simpler way of setting the color

    CGContextSetFillColorWithColor(ctx, [[UIColor redColor] CGColor]);

    CGContextFillPath(ctx);
}

This way, the location and size of the circle will be dictated by the frame I set for the circle (by the way, I apologize if this makes it more confusing, but I use a different class name, CircleView, as I like to have View in the name of my UIView subclasses).

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIView *circle;

    circle = [[CircleView alloc] initWithFrame:CGRectMake(100.0, 100.0, 200.0, 200.0)];
    circle.backgroundColor = [UIColor clearColor]; // because it calls `super drawRect` I can now enjoy standard UIView features like this
    [self.view addSubview:circle];

    circle = [[CircleView alloc] initWithFrame:CGRectMake(300.0, 300.0, 10.0, 10.0)];
    circle.backgroundColor = [UIColor blackColor]; // because it calls `super drawRect` I can now enjoy standard UIView features like this
    [self.view addSubview:circle];
}
like image 72
Rob Avatar answered Sep 30 '22 07:09

Rob