簡短的故事是我希望NSBezierPath的邊界(我認爲我的意思是邊界而不是框架)填充視圖。例如: 用路徑翻譯視圖(ObjectiveC,Cocoa)
要生成上述圖像,我使用Covert latitude/longitude point to a pixels (x,y) on mercator projection中的信息縮放/轉換了我創建的路徑中的每個點。問題是這不是可縮放的(我將添加更多的路徑),我想輕鬆地將pan/zoom功能添加到我的視圖中。此外,我希望筆畫保持不變,而不管比例(即縮放時沒有脂肪邊界)。
我想我想在某些任意參考幀(例如經度和修改的緯度)中生成一個可重用路徑,而不是每次窗口更改時生成一個新路徑。然後,我可以翻譯/縮放視圖的座標系以填充視圖。
所以我用Apple's geometry guide來修改視圖的框架。我獲得了翻譯的權利,但縮放失敗。
[self setBoundsOrigin:self.path.bounds.origin];
[self scaleUnitSquareToSize:NSMakeSize(1.5, 1.5)];
然後我在drawRect中嘗試了coordinate system transformation:方法只用了類似的結果告終。
NSAffineTransform* xform = [NSAffineTransform transform];
[xform translateXBy:(-self.path.bounds.origin.x) yBy:(-self.path.bounds.origin.y)];
[xform scaleXBy:1.5 yBy:1.5];
[xform concat];
最後我試圖手動設置在drawRect中的視圖範圍:但結果卻是醜陋,很慢!
我知道我還可以改變NSBezierPath對象,我認爲這工作,但我寧願變換角度,而不是一次通過循環和轉化每個路徑的每次更新。我認爲,我錯過了大約三行代碼,這些代碼將完全符合我的要求。
編輯: 這裏的drawRect:方法我用:
- (void)drawRect:(NSRect)dirtyRect
{
// NSAffineTransform* xform = [NSAffineTransform transform];
// [xform translateXBy:-self.path.bounds.origin.x yBy:-self.path.bounds.origin.y];
// [xform scaleXBy:1.5 yBy:1.5];
// [xform concat];
[self drawBoundaries];
NSRect bounds = [self bounds];
[[NSColor blackColor] set];
[NSBezierPath fillRect:bounds];
// Draw the path in white
[[NSColor whiteColor] set];
[self.path stroke];
[[NSColor redColor] set];
[NSBezierPath strokeRect:self.path.bounds];
NSLog(@"path origin %f x %f",self.path.bounds.origin.x, self.path.bounds.origin.y);
NSLog(@"path bounds %f x %f",self.path.bounds.size.width, self.path.bounds.size.height);
}
難道你不想改變邊界的大小,而不是原點? –
路徑的邊界起點爲-124.87 x 25.17,大小爲58.01 x 31.79。 –
我的意思是如果你想縮放繪圖,你應該改變邊界,但是這也會縮小線條粗細......我們可以看到你的'drawRect:'實現嗎? –