【发布时间】:2012-08-30 23:44:00
【问题描述】:
我正在尝试使用自定义视图使我的主 ViewController 将多个自定义 UIView 绘制到主视图,但不知何故它们没有绘制,我正在尝试绘制点。
我的代码是:
- (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];
}
但这不起作用,我想知道我的 CustomDotView 的构造函数是正确的还是我做错了什么
这是我的 CustomDotView 构造函数
-(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);
}
有什么建议吗?
【问题讨论】:
-
你必须给你的 CustomDotView 一个框架。现在,您正尝试在 0x0 矩形内绘制。请务必同时致电超级
initWithFrame。 -
我应该用点的尺寸创建一个 CGRectMake 对吗?我可以在运行时更改它吗?
-
是的,如果您愿意,您甚至可以在框架外绘制。只需确保在您的自定义视图上关闭
clipSubviews。而不是在你的绘图循环中创建currentRect,只需在你的初始化中创建它,然后它将作为drawRect:中的参数传递给 -
我正在创建矩形,到目前为止,我在屏幕顶部看到一个黑色矩形,但没有看到带颜色的绘制点,而且我在 CGRect rect 的任何地方都找不到 clipSubviews 属性= CGRectMake(10, 10, 10, 10); self = [super initWithFrame:rect];
标签: ios uiview uiviewcontroller core-graphics quartz-graphics