开发者

NSTrackingArea yields different results when initialized with initWithRect:[self bounds] and [self frame]

--------- Question 1 ---------- Maybe I don't understand the frame object completely (or maybe it wasn't created correctly in my context), b开发者_JAVA技巧ut as I was trying to implement a mouse event I encountered the situation where the call:

trackingArea=[ [NSTrackingArea alloc] initWithRect:[self frame] options:(NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveInKeyWindow) owner:self userInfo:nil ];

Resulted in incorrect tracking area (only top right corner worked) - looking into the problem I found that 'frame' started somewhere in top right corner. This call was made in an overloaded initWithFrame defined as:

glView = [ [ Lesson17View alloc ] initWithFrame:[ glWindow frame ]
colorBits:16 depthBits:16 fullscreen:FALSE ];

where glView is subclassed from NSOpenGLView. The above call is made in awakeFromNib, and glWindow is defined as:

IBOutlet NSWindow *glWindow;

When I replace bounds with frame, my application works - what is going on with the frame variable?

--------- Question 2 ----------

My second question involves how to handle the mouse events and correctly have my openGL view redraw - say I have:

- (void) mouseMoved:(NSEvent *)theEvent
{
NSPoint location=[self convertPoint:[theEvent locationInWindow] fromView:nil];
// send NSPoint to a printGL function I have that uses texture maps to display text?
}

I'm guessing I shouldn't be sending this to a function that renders something in my openGL view (because this doesn't do anything -guessing because whenever I use drawRect whatever I drew during the mouse event is cleared). Currently all my drawing is perfomed in drawRect (contains a bunch of openGL calls) - what would be the appropriate call to make at this point?

Or should I set a flag (along with setting an instance variable NSPoint) to true that will allow printGL to be called only on mouseMoved events (I know - I'll use the mouseEntered/Exited functions but for now I'm trying to get a handle on this).


A view's frame is where the view is located in its superview. This means most importantly that unless the view's origin is right on top of its superview's origin (i.e. at {0, 0}), the frame rectangle will be offset by that amount.

A view's bounds are the coordinates inside the view. Unless you've applied some transform to the view's coordinate system, the bounds origin will always be at {0, 0}.

So, why does this make a difference in your case? As noted in the docs for NSTrackingArea, the rect is interpreted as coordinates in the coordinate system of the tracking area's owner. The owner's bounds are in its coordinate system, while the frame is in coordinate system of the owner's superview. So you're giving the tracking area a rect from the wrong coordinate system.

I highly recommend reading the View Programming Guide. It explains all of this and more. There are a lot of subtle details to the Cocoa view system that you'll want to know when you're writing UI code.

(And for your second question: To tell a view to redraw, send it [view setNeedsDisplay:YES].)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜