开发者

mouse event location appears to be incorrect

I've got a simple test app with a custom view (set up in Interface Builder) with its origin at (20, 20). When I get a mouse event at the lower left most point in the view, the event开发者_运维知识库's location is being reported as (20, 21) and the converted point at (0, 1). I'm using pixie to make sure I'm clicking right at the lower left corner. (If I move one pixel down, I get nothing, indicating I'm outside the view.)

Here's the mouseDown code:

- (void)mouseDown:(NSEvent *)e
{
    NSPoint pt = [e locationInWindow];
    NSLog(@"Location in Window: %.0f, %.0f", pt.x, pt.y);

    pt = [self convertPoint:pt fromView:nil];

    NSLog(@"Converted Point: %.0f, %.0f", pt.x, pt.y);
}

Can anyone explain why the y position appears to be off by one?


This is correct behavior. From the Cocoa Drawing Guide:

Important: Cocoa event objects return y coordinate values that are 1-based instead of 0-based. Thus, a mouse click on the bottom left corner of a window or view would yield the point (0, 1) in Cocoa and not (0, 0). Only y-coordinates are 1-based.


Fencepost error? In your code, or the library or your use of the library? Rounding errors as you are doing integral pixel reporting of floating point data?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜