我正在绘制一个非结构化的二叉树。我现在遇到的问题是,当我构建应用程序时,它会进入无限循环,一直创建新的nodeView对象。
我是编程绘图的新手。我知道我犯了一些可能相当明显的基本错误。在不经常重新实现这些的情况下绘制NSView树的最佳方法是什么。
理想情况下,我希望能够拖放NSView节点。我可以在以后想出这个信息,以防它以某种方式帮助。
- (void)drawRect:(NSRect)dirtyRect {
if (!hasSetUp) [self setUp];
[super drawRect:dirtyRect];
DTNodeView *nodeView = [[DTNodeView alloc] initWithFrame:NSMakeRect(200, 200, 100, 100)];
//[self addSubview:nodeView];
[[NSColor yellowColor] set];
NSRectFill([self frame]);
//NSLog(@"Subviews are: %@", [self.window.contentView subviews]);
NSLog(@"Left tree height: %i", leftTree.treeHeight);
double deltaY = (self.window.frame.size.height / 2) / ([leftTree treeHeight] + 1);
[self drawSubtreeFromNode:[leftTree root] yValue:self.window.frame.size.height - 100 yDecrement:deltaY leftBound:0 rightBound:self.window.frame.size.width / 2];
// Drawing code here.
}
-(void)drawSubtreeFromNode:(DTNode *)node yValue:(double)y yDecrement:(double)deltaY leftBound:(double)left rightBound:(double)right {
double x = (left + right) / 2; // Draw the node in between the parents
DTNodeView *selfRoot = [[DTNodeView alloc] initWithFrame:NSMakeRect(x, y, 100, 50) andNode:node];
[self addSubview:selfRoot];
if ([node hasLeft]) {
double y2 = (y - deltaY);
double x2 = (left + ((left + right)/2))/2;
[[NSColor redColor] set];
NSBezierPath *line = [[NSBezierPath alloc] init];
[line setLineWidth:2];
[line moveToPoint:NSMakePoint(x, y)];
[line lineToPoint:NSMakePoint(x2, y2)];
[line stroke];
//drawSubtree(node.getLeft(), y-yDecrement, yDecrement, left, ((left + right)/2));
[self drawSubtreeFromNode:[node left] yValue:(y - deltaY) yDecrement:deltaY leftBound:left rightBound:((left + right) / 2)];
}
if ([node hasRight]) {
double x2 = (right + ((left + right)/2))/2;
double y2 = (y - deltaY);
[[NSColor redColor] set];
NSBezierPath *line = [[NSBezierPath alloc] init];
[line setLineWidth:2];
[line moveToPoint:NSMakePoint(x, y)];
[line lineToPoint:NSMakePoint(x2, y2)];
[line stroke];
//drawSubtree(node.getRight(), y-yDecrement, yDecrement, ((left + right)/2), right);
[self drawSubtreeFromNode:[node right] yValue:(y - deltaY) yDecrement:deltaY leftBound:((left + right) / 2) rightBound:right];
}
}
答案 0 :(得分:2)
在drawRect方法中,您应该绘制而不是其他内容。它不是创建视图并将其添加到超级视图的地方。很明显,添加子视图意味着需要重新绘制视图,然后创建新的子视图等等。