SetNeedsDisplay does not start DrawRect

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

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

Using drawCircle, I would like to be able to draw my MapViewController at any time. But I'm not sure I understand the concept of this. I know that my drawing code works, I used it before. But I do not know how to draw on a MapViewController using DrawCircle .

From what it seems to me, when I call [myCustomView setNeedsDisplay] , it does not call the DrawRect method in my view.

Here is the 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 helps me in the process of my thought, here is my StoryBoard scene. Storyboard board

If the custom viewcontrollers class is a MapViewController and the custom view class is DrawCircle.

** EDIT: ** I would also like to mention that in my DrawCircle.m I have methods that I call from MapViewController.m and work.

Besides. The DrawRect method is called first, but I cannot manually call using setNeedsUpdate . When debugging, it does not even introduce the DrawRect method.

+6
source share
1 answer

You create your DrawCircle, but you never add it to the view, for example.

 [self.view addSubview:circleView]; 

Therefore, it goes out of scope and (if using ARC) is freed from you. You also do not set its frame , for example:

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

Alternatively, you can add a view to the interface constructor (standard UIView , but then specify your own class in IB).

Also, note that you usually don’t even call setNeedsDisplay . Adding this to the view hierarchy will cause this for you. You only need to call this if you need to update the view based on some custom property.


Personally, I would be inclined to define drawRect as follows:

 - (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 set for the circle (by the way, I apologize if this makes it more confusing, but I use a different class name, CircleView , because I like 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]; } 
+5
source

Source: https://habr.com/ru/post/944015/


All Articles