简短的故事是我希望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:'实现吗? –