1

我有一个应用程序,其中一个视图与 MS Visio 配置类似。您可以添加不同的节点(圆圈,从 UIImage 渲染),然后用线条连接它们,以创建类似于树数据表示的东西。添加部分工作正常。

-(void) drawLineToCache{

   // Circle *dad;
   // Circle *kid; 
   //      Circle is a wrapper class for the nodes.. dad and kid are   
   //      private instances declared earlier in this paint view

//associatedPaths is a NSMutableArray that contains Line objects, 
//       which is simply a wrapper class with a CGPathRef
    [dad.associatedPaths addObject:self.selectedLine]; 
    [kid.associatedPaths addObject:self.selectedLine]; 

    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, dad.middlePoint.x, dad.middlePoint.y);
    CGPathAddLineToPoint(path, NULL, kid.middlePoint.x, kid.middlePoint.y);
    CGPathCloseSubpath(path);
    CGContextAddPath(cacheContext, path);
    CGContextSetStrokeColorWithColor(cacheContext,[UIColor blackColor].CGColor);
    CGContextStrokePath(cacheContext);
    CGPathRelease(path);

    CGRect dirtyRect = CGRectMake(dad.middlePoint.x-10, dad.middlePoint.y-10, 500, 400);
    [self setNeedsDisplayInRect:dirtyRect];

}

我现在想要做的是移动圆圈,然后删除它们。由于圆圈是 UIImageViews,因此只需更新框架/将其从子视图中删除即可移动/删除它们。

然后为了更新它,我尝试了:

//Ideally this should be implemented in touchesMoved, but I 
//thought it might be too much for the renderer.. 
// movingCircle is simply a reference to which circle needs to be updated 
// (the above kid/dad). I have been able to debug it to the point that the 
// associatedPath arrays contains the correct lines.. 
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    CGPoint touchLocation = [[touches anyObject] locationInView:self];

    for (Line *line in movingCircle.associatedPaths) {
        CGMutablePathRef path = CGPathCreateMutableCopy(line.path);
        CGPathMoveToPoint(path, NULL, line.parentNode.middlePoint.x, line.parentNode.middlePoint.y);
        CGPathAddLineToPoint(path, NULL, line.childNode.middlePoint.x, line.childNode.middlePoint.y);
        CGPathCloseSubpath(path);
        CGContextAddPath(cacheContext, path);
        CGContextSetStrokeColorWithColor(cacheContext,[UIColor blackColor].CGColor);
        CGContextStrokePath(cacheContext);
        CGPathRelease(path);


    }
    [self setNeedsDisplay];
    movingCircle = nil;
}

但它不起作用......我知道这只会画一条新线(而不是删除旧线),但甚至不会做那么多......我也零知道如何删除旧线。 ..

我的问题是:如何对线条做同样的事情?我可以很乐意将每个路径的每个路径存储在一个数组中,但即便如此,在访问路径之后,我该如何更新它们?我需要画一个新的,并以某种方式删除旧的吗?或者我可以简单地更改线的端点之一吗?

提前致谢!:)

4

1 回答 1

0

将它们存储在一个可变数组中,并简单地替换任何需要更改的行。您可以考虑为每个存储和应用转换,但这会更复杂,并且可能不会产生如此清晰的结果。一旦您更新了阵列中的路径,您只需重新绘制屏幕的受影响区域。

您可能还想使用贝塞尔路径而不是 CG 路径(更明亮的 API 与您正在使用的图像视图更具可比性)。

于 2013-11-01T08:15:28.237 回答