Subclassed UIView绘制多边形

时间:2013-05-22 22:46:23

标签: ios objective-c uiview

我有一个子类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:时仍然存在问题,它是零。 /惊奇

0 个答案:

没有答案