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.
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.
MKMapView
and if you want this view to be associated with lat/long coordinates, there are much better ways of accomplishing that (e.g. custom annotation orMKCircle
overlay). But if you want this red circle to float in space, not moving as the user pans and zooms the map, then your custom view is fine. Just depends upon what you're trying to do. – Rob