という 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;
}
これを実行すると、行は表示されず、ノードのみが表示されます。よろしくお願いします。