2011-09-29 218 views
4

我有一个NSView的子类,重新实现了一些鼠标事件函数。例如,在鼠标按下得到来自NSEvent我用了一点:翻转NSView鼠标坐标

NSEvent *theEvent; // <- argument to function 

NSPoint p = [theEvent locationInWindow]; 
p = [self convertPoint:p fromView:nil]; 

然而坐标似乎翻转,(0,0)是左下角的窗口?

编辑:我已经重写了isFlipped方法返回TRUE,但它只影响绘图。对不起,我不能相信我忘了把这一切放在一边。

回答

5

翻转是什么意思? Mac对所有内容都使用LLO(左下角)坐标系。

编辑我不能用一个简单的项目重现这一点。我创建了一个这样实现的单个NSView

@implementation FlipView 
- (BOOL)isFlipped { 
    return YES; 
} 

- (void)mouseDown:(NSEvent *)theEvent { 
    NSPoint p = [theEvent locationInWindow]; 
    p = [self convertPoint:p fromView:nil]; 
    NSLog(@"%@", NSStringFromPoint(p)); 
} 
@end 

我收到了我期望的坐标。删除isFlipped按预期切换方向。你有一个简单的项目来证明你的问题吗?

+0

我的意思是,我已经覆盖了isFlipped方法。我的错,我没有说清楚。 – CD1212

+0

重写'-isFlipped'方法对我也不适用。 –

+2

在我的测试中,如果您使用convertPoint:** fromView:**,则'isFlipped {YES}'*将*将点转换为左上角原点。但*不*如果您使用convertPoint:** toView:** - 这是预期的,因为'toView:'假定该点已经在'self'视图的坐标系中。 –

2

这并不是“翻转”的,必然的,这就是石英如何协调。摘自Quartz 2D上的文档:

用户空间中的点由坐标对(x,y)表示,其中x表示沿着水平轴(左侧和右侧)的位置,y表示垂直轴(上和下)。用户坐标空间的原点是点(0,0)。原点位于页面的左下角,如图1-4所示。在Quartz的默认坐标系中,x轴从页面的左侧向右侧移动时增加。随着从底部向页面顶部移动,y轴的值会增加。

虽然我不确定你的问题是什么。你在寻找一种获得“翻转”坐标的方法吗?如果是这样,你可以继承你的NSView,覆盖-(BOOL)isFlipped方法返回YES。

2

我发现这很令人讨厌 - 直到有一天,我只是坐下来,拒绝起床,直到我有一个完美的工作。这..通过被称为...

-(void) mouseDown:(NSEvent *)click{ 
    NSPoint mD = [NSScreen wtfIsTheMouse:click 
         relativeToView:self]; 
} 

调用上NSScreen类别....

@implementation NSScreen (FlippingOut) 
+ (NSPoint)wtfIsTheMouse:(NSEvent*)anyEevent 
      relativeToView:(NSView *)view { 
    NSScreen *now = [NSScreen currentScreenForMouseLocation]; 
    return [now flipPoint:[now convertToScreenFromLocalPoint:event.locationInWindow relativeToView:view]]; 
} 
- (NSPoint)flipPoint:(NSPoint)aPoint { 
     return (NSPoint) { aPoint.x, 
    self.frame.size.height - aPoint.y }; 
} 
- (NSPoint)convertToScreenFromLocalPoint:(NSPoint)point 
     relativeToView:(NSView *)view { 
    NSPoint winP, scrnP, flipScrnP; 
    if(self) { 
     winP = [view convertPoint:point toView:nil]; 
     scrnP = [[view window] convertBaseToScreen:winP]; 
     flipScrnP = [self flipPoint:scrnP]; 
     flipScrnP.y += [self frame].origin.y; 
     return flipScrnP; 
     } return NSZeroPoint; 
} 
@end 

希望这样可以防止只是一个次要freakout ... 地方,总有一天 。对于孩子们来说,该死的。我为你的孩子求求你。

2

此代码为我工作:

NSPoint location = [self convertPoint:theEvent.locationInWindow fromView:nil]; 
location.y = self.frame.size.height - location.y;