2013-11-23 93 views
0

簡短的故事是我希望NSBezierPath的邊界(我認爲我的意思是邊界而不是框架)填充視圖。例如: map用路徑翻譯視圖(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]; 

map fail

[self scaleUnitSquareToSize:NSMakeSize(1.5, 1.5)]; 

map fail

然後我在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]; 

map fail again

最後我試圖手動設置在drawRect中的視圖範圍:但結果卻是醜陋,很慢!

arg

我知道我還可以改變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); 
} 
+0

難道你不想改變邊界的大小,而不是原點? –

+0

路徑的邊界起點爲-124.87 x 25.17,大小爲58.01 x 31.79。 –

+0

我的意思是如果你想縮放繪圖,你應該改變邊界,但是這也會縮小線條粗細......我們可以看到你的'drawRect:'實現嗎? –

回答

0

我能得到它用兩個轉變工作。當我有很多轉換路徑和一個放大/縮小的窗口時,我試圖避免這種情況,以減少複雜性和計算量。

- (void)transformPath:(NSBezierPath *)path 
{ 
NSAffineTransform *translateTransform = [NSAffineTransform transform]; 
NSAffineTransform *scaleTransform = [NSAffineTransform transform]; 

[translateTransform translateXBy:(-self.path.bounds.origin.x) 
          yBy:(-self.path.bounds.origin.y)]; 

float scale = MIN(self.bounds.size.width/self.path.bounds.size.width, 
        self.bounds.size.height/self.path.bounds.size.height); 
[scaleTransform scaleBy:scale]; 

[path transformUsingAffineTransform: translateTransform]; 
[path transformUsingAffineTransform: scaleTransform]; 
}