1

我有一个子类UIView

@implementation draw2D

- (id)initWithFrame:(CGRect)frame andPointArray:(NSArray *)pointArray
{
    DLog(@"fired");

    DLog(@"pointArray: %@", pointArray);

    DLog(@"frame: %@", NSStringFromCGRect(frame));

    self = [super initWithFrame:frame];
    if (self) {
        DLog(@"self is valid");
        self.pointArray = pointArray;
        DLog(@"self.pointArray: %@", self.pointArray);
    }
    else {
        DLog(@"self is invalid");
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    DLog(@"fired");
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(context, self.lineWidth);
    CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);    

    DLog(@"self.pointArray: %@", self.pointArray);

    for (NSDictionary *pointInArray in self.pointArray) {
        CGPoint point = CGPointMake([[pointInArray objectForKey:@"x"] floatValue], [[pointInArray objectForKey:@"y"] floatValue]);

        DLog(@"point: %@", NSStringFromCGPoint(point));

        if ([pointInArray isEqualToDictionary:[self.pointArray objectAtIndex:0]]) {
            DLog(@"first point");
            CGContextMoveToPoint(context, point.x, point.y);
        }
        else {
            DLog(@"all other points");
            CGContextAddLineToPoint(context, point.x, point.y);
        }
    }

    CGContextStrokePath(context);
}

我创建UIView

draw2D *fieldImage = [[draw2D alloc] initWithFrame:fieldCell.fieldImage.frame andPointArray:[NSArray arrayWithArray:points]];
fieldImage.backgroundColor = [UIColor blueColor];
fieldImage.lineColor = [UIColor redColor];
fieldImage.lineWidth = 2;

问题是,在 中drawRect:,我self.pointArray的为零:

[draw2D initWithFrame:andPointArray:] | self.pointArray: (
    {
        x = 108;
        y = "105.6408";
    },
        {
        x = "27.35526";
        y = 108;
    },
        {
        x = 27;
        y = "103.5437";
    },
        {
        x = 0;
        y = "103.2816";
    },
        {
        x = "8.526316";
        y = "52.68932";
    },
        {
        x = "45.82895";
        y = "4.194175";
    },
        {
        x = "106.9342";
        y = 0;
    }
)

[draw2D drawRect:] | fired
[draw2D drawRect:] | self.pointArray: (null)

我究竟做错了什么?或者有没有更好的方法来绘制一个简单的多边形?

编辑:

@interface draw2D : UIView

@property (nonatomic) CGPoint startPoint, endPoint;
@property (nonatomic) float lineWidth;
@property (strong, nonatomic) UIColor *lineColor;
@property (strong, nonatomic) NSArray *pointArray;

- (id)initWithFrame:(CGRect)frame andPointArray:(NSArray *)pointArray;

@end

编辑2:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        FieldInfoCell *fieldCell = [tableView dequeueReusableCellWithIdentifier:FieldInfoCellIndentifier forIndexPath:indexPath];

        // Set up the cell

        //fieldImage is a draw2D UIView
        fieldCell.fieldImage = [self drawMapImageForFieldCell:fieldCell];

        DLog(@"fieldCell.fieldImage.pointArary: %@", fieldCell.fieldImage.pointArray); // Point array is still valid

        return fieldCell;
    }
}

-(draw2D *)drawMapImageForFieldCell:(FieldInfoCell *)fieldCell {
    // Creates the points array

    draw2D *fieldImage = [[draw2D alloc] initWithFrame:fieldCell.fieldImage.frame andPointArray:[NSArray arrayWithArray:points]];
    fieldImage.backgroundColor = [UIColor blueColor];
    fieldImage.lineColor = [UIColor redColor];
    fieldImage.lineWidth = 2;

    DLog(@"fieldImage.pointArary: %@", fieldImage.pointArray); //Point array is still valid

    return fieldImage;
}

但在 中drawRect:,pointArray 为零。/惊奇

如果我将这些点硬编码到drawRect:方法中,它会按预期绘制。/惊奇

该日志打印出点数组。当它到达时仍然有问题drawRect:,它为零。/惊奇

4

0 回答 0