1
我有一個應用程序,其中一個視圖與MS Visio配置類似。您可以添加不同的節點(圓形,從UIImage渲染),然後用線連接它們,以創建類似於樹形數據表示的東西。添加部分工作正常。如何移動/重繪CGPathRef?
-(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;
}
但它不工作...我知道,這隻會繪製新的(而不是刪除舊的),但甚至不會做那麼多..我也沒有想法如何刪除舊的...
我的問題是:如何做到與行相同?我可以很高興地將每個路徑存儲在一個數組中,但是即使如此,在訪問路徑之後,我如何更新它們?我需要繪製一個新的,並以某種方式刪除舊的?或者我可以簡單地改變線路的一個端點嗎?
在此先感謝! :)
你能告訴我一些示例代碼嗎?我試過這樣做,並沒有很成功 – Cutetare
UIBezierPath的優勢是什麼?我看着這個班級,認爲這對我所做的事情來說太複雜了,但我對CG很新。 – Cutetare
這意味着你不需要和CG上下文交互,只需設置填充顏色用UIColor,然後中風/填充貝塞爾。 – Wain