tags:

views:

339

answers:

1
+2  Q: 

convertPoint:

Hi

I'm learning Objective-C and Cocoa (in fits and starts when time allows) so be gentle OK.

A example app has the following lines:

NSPoint down = [mouseEvent locationInWindow];
//...some other stuff
NSPoint p = [self convertPoint:down fromView:nil];

It then proceeds to use p for a drag and drop operation (using the pasteBoard). But, what I don't get is this is all in one view, why not just use down, why do a convertPoint: ? Or have I missed something basic?

Thanks!

+4  A: 

The point "down" is in the window's coordinate system — it starts in the corner of the window. The point "p" is in the view's coordinate system. Unless this view's origin happens to be exactly at the window's origin and neither has had its coordinate system transformed (say, with setBounds:), the two points will not be in the same place.

Chuck
Thanks, appreciate the help. I did notice the results were different but could not figure why.
Joe
Chuck, I continuously have to visit this through trial and error. Your explanation was very concise and made sense to me such that I was able to jump back to my code and instantly made the right transformation after getting it wrong again and again for about three hours! I voted your answer up.
mobibob