设置新原点后,NSView边界不会改变

时间:2014-10-12 14:03:29

标签: macos cocoa positioning nsview zooming

我有一个自定义的NSView,可以缩放或放大放大事件中的图像。但是,我注意到即使在设置新的边界大小和原点之后,只有大小似乎对正在显示的内容产生影响:

enter image description here

当我不想要时,边界原点似乎粘在左下角(0,0)。我更新边界大小和原点覆盖 - (void)magnifyWithEvent:

-(void)magnifyWithEvent:(NSEvent *)event {

    CGFloat zoomFactor = [event magnification] + 1.0;

    CGSize newSize = [self bounds].size;

    newSize.width = newSize.width * zoomFactor;
    newSize.height = newSize.height * zoomFactor;

    [self setBoundsSize:newSize];

    CGSize viewFrameSize = [self frame].size;
    CGPoint newOrigin;
    newOrigin.x = (viewFrameSize.width - newSize.width) / 2.0;
    newOrigin.y = (viewFrameSize.height - newSize.height) / 2.0;

    [self setBoundsOrigin:newOrigin];

    [self setNeedsDisplay:YES];

}

控制台日志中的打印输出显示边界原点正在改变:

2014-10-12 09:50:00.315 ZoomImageViewer[8047:303] Bounds origin x: 105.317268 y: 78.987951
2014-10-12 09:50:00.316 ZoomImageViewer[8047:303] Frame origin x: 0.000000 y: 0.000000
2014-10-12 09:50:00.333 ZoomImageViewer[8047:303] Bounds origin x: 106.124921 y: 79.593691
2014-10-12 09:50:00.333 ZoomImageViewer[8047:303] Frame origin x: 0.000000 y: 0.000000
2014-10-12 09:50:00.347 ZoomImageViewer[8047:303] Bounds origin x: 106.927730 y: 80.195798
2014-10-12 09:50:00.347 ZoomImageViewer[8047:303] Frame origin x: 0.000000 y: 0.000000

有没有办法让界限集中在我想要的位置,或者我误解了它应该如何运作?

0 个答案:

没有答案