我有一个 viewController,它显然是 UIViewController
的子类,名为 MapViewController
。在此 viewController 中,我使用 GPS 获取用户的位置。
我还有一个名为 DrawCircle
的 View 。此 View 是 UIView
的子类。
使用 drawCircle 我希望能够随时在我的 MapViewController 上绘图。但我不确定我是否理解这样做的概念。我知道我的绘图代码正在运行,我以前用过它。但我不知道如何使用 DrawCircle
在 MapViewController
上绘制。
在我看来,每当我调用 [myCustomView setNeedsDisplay]
时,它并没有在我的 View 中调用 DrawRect
方法。
这是一些代码: 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);
}
此外,如果这对我的思维过程有任何帮助,这是我的 StoryBoard 场景。
viewcontrollers 自定义类是 MapViewController,views 自定义类是 DrawCircle。
**编辑:**我还想提一下,在我的 DrawCircle.m 中,我有从 MapViewController.m 调用的方法并且正在工作。
还有。最初,正在调用 DrawRect 方法,但我无法使用 setNeedsUpdate
手动调用。调试的时候连DrawRect方法都没有进入。
最佳答案
你正在创建你的 DrawCircle,但你永远不会将它添加到 View 中,例如
[self.view addSubview:circleView];
因此,它超出了范围,并且(如果使用 ARC)会向您发布。您似乎也没有设置它的 frame
,例如:
circleView = [[DrawCircle alloc] initWithFrame:self.view.bounds];
或者,您可以在界面生成器中添加 View (一个标准的 UIView
,然后在 IB 中指定您的自定义类)。
另外请注意,您通常甚至不需要调用 setNeedsDisplay
。将它添加到 View 层次结构将为您调用它。如果您需要根据某些自定义属性更新 View ,您只需要调用它。
就我个人而言,我倾向于这样定义 drawRect
:
- (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);
}
这样,圆圈的位置和大小将由我为圆圈设置的 frame
决定(顺便说一句,如果这使它更困惑,我深表歉意,但我使用了不同的类名 CircleView
,因为我喜欢在我的 UIView
子类的名称中包含 View
。
- (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];
}
关于ios - setNeedsDisplay 不触发 DrawRect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16312168/