【问题标题】:Draw multiple UIView objects dynamically on the current UIView在当前 UIView 上动态绘制多个 UIView 对象
【发布时间】: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


【解决方案1】:

好的,我继续在 Xcode 中写了这个,效果很好。这是我能想到的最简单的用例;这是在空白UIViewController 内。

@implementation ViewController

- (void)viewDidLoad
{

    CustomDotView *newDot = [[CustomDotView alloc] initWithPointAtXCord:10 andYCord:10 withRadius:10 andColor:[UIColor redColor]];

    [self.view addSubview:newDot];

}

@end


@implementation CustomDotView

-(id)initWithPointAtXCord:(float)inputXCoord andYCord:(float)inputYCoord withRadius:(float)inputRadius andColor:(UIColor *)inputColor
{
    xCoord = inputXCoord;
    yCoord = inputYCoord;
    color = inputColor;
    radius = inputRadius;

    self = [super initWithFrame: CGRectMake(xCoord, yCoord, radius * 2, radius * 2)];

    self.backgroundColor = [UIColor clearColor];

    return self;

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

    CGContextSetLineWidth(context, 1.0);
    CGContextSetStrokeColorWithColor(context, color.CGColor);
    CGContextSetFillColorWithColor(context, color.CGColor);


    CGContextAddEllipseInRect(context, rect);
    CGContextDrawPath(context, kCGPathFill);
}


@end

如果您想移动 Dot,只需更改它的框架即可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    相关资源
    最近更新 更多