0

という UIView サブクラスがありMENodeます。それぞれに、そのサブノード (およびs)MENodeを追跡する NSMutableDictionary があります。MENode私が達成しようとしているのは、親ノードから子ノードに線を引くことです。drawRect:これはinの私の実装ですMENode:

- (void)drawRect:(CGRect)rect{
//connect the sub node to its super node by drawing a line between the two;

    for (int i=0; i<self.subNodes.count; i++) {
        //get one of the child nodes
        MENode *subNode = [self.subNodes objectAtIndex:i];

        //get the context and set up color and line width
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
        CGContextSetLineWidth(context, 5);

        //set up a line between the parent node and the child node
        CGContextMoveToPoint(context, self.center.x, self.center.y);
        CGContextAddLineToPoint(context, subNode.center.x, subNode.center.y);

        //draw it.
        CGContextStrokePath(context);
    }
}

以下のメソッドは両方とも、View Controller によって呼び出されます。私は両方で呼び出します[self setNeedsDisplay];

-(void)addNode:(MENode *)newNode{
    //set the child node's parent node to self (a weak reference)
    newNode.parentNode = self;

    //add the node to self.subNodes
    [self.subNodes addObject:newNode];

    //add the node to the parent node
    [self addSubview:newNode];

    //call seeNeedsDisplay
    [newNode setNeedsDisplay];
}

-(void)nodeAdditionsDone{
    //call setNeedsDisplay
    [self setNeedsDisplay];

    //do some logic with this later
    self.nodeAddtionsAreFinished = YES;
}

これを実行すると、行は表示されず、ノードのみが表示されます。よろしくお願いします。

4

1 に答える 1

1
CGContextMoveToPoint(context, self.center.x, self.center.y);

self.centerビューのスーパービューの座標空間にあるため、それが問題の一部である可能性があります。これを試して:

CGContextMoveToPoint(context, CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds))

描画コードにブレークポイントを設定して、self.subNodes描画の実行時に実際にオブジェクトがあることを確認することもできます。

于 2014-01-30T23:00:58.390 に答える