wrj*_*hns 8 cocoa nsimage nsimageview
我有一个占用窗口全部范围的NSImageView.图像视图没有边框,其设置显示在左下角.因此,这意味着视图的原点与实际图像的原点匹配,无论窗口的大小如何调整.
此外,图像比我在屏幕上完全适合的尺寸大得多.所以我也将imageview设置为按比例缩小图像的大小.但是,我似乎无法在任何地方找到这种比例因子.
我的最终目标是将鼠标按下事件映射到实际图像坐标.要做到这一点,我想我还需要一条信息......显示的NSImage实际上有多大.
如果我看一下[imageView bounds],我会得到图像视图的边界矩形,它通常会比图像大.
我认为这给了你你所需要的:
NSRect imageRect = [imageView.cell drawingRectForBounds: imageView.bounds];
Run Code Online (Sandbox Code Playgroud)
它返回视图中图像原点的偏移量及其大小。
对于您重新映射鼠标坐标的最终目标,您的自定义视图类上的类似内容应该可以工作......
- (void)mouseUp:(NSEvent *)event
{
NSPoint eventLocation = [event locationInWindow];
NSPoint location = [self convertPoint: eventLocation fromView: nil];
NSRect drawingRect = [self.cell drawingRectForBounds:self.bounds];
location.x -= drawingRect.origin.x;
location.y -= drawingRect.origin.y;
NSSize frameSize = drawingRect.size;
float frameAspect = frameSize.width/frameSize.height;
NSSize imageSize = self.image.size;
float imageAspect = imageSize.width/imageSize.height;
float scaleFactor = 1.0f;
if(imageAspect > frameAspect) {
///in this case image.width == frame.width
scaleFactor = imageSize.width / frameSize.width;
float imageHeightinFrame = imageSize.height / scaleFactor;
float imageOffsetInFrame = (frameSize.height - imageHeightinFrame)/2;
location.y -= imageOffsetInFrame;
} else {
///in this case image.height == frame.height
scaleFactor = imageSize.height / frameSize.height;
float imageWidthinFrame = imageSize.width / scaleFactor;
float imageOffsetInFrame = (frameSize.width - imageWidthinFrame)/2;
location.x -= imageOffsetInFrame;
}
location.x *= scaleFactor;
location.y *= scaleFactor;
//do something with you newly calculated mouse location
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3949 次 |
| 最近记录: |