0

I’m trying to do a simple animation of an NSView. The view had some weird "jumping" problems and I managed to strip my code down to

[editView setFrame:NSMakeRect([editView bounds].origin.x,
    [editView bounds].origin.y,
    [editView bounds].size.width,
    [editView bounds].size.height)];

and it still produces the "jummping" problem. When I run this, I would expect the view to not move at all but on the first and only the first time, it jumps to the left about 200px. What could possibly be going wrong?

nosedive25
  • 2,477
  • 5
  • 30
  • 45

3 Answers3

2

Frame and bounds aren't the same thing. A view's frame is its position and size in its superview's co-ordinate system, whereas its bounds is the origin and size of its own co-ordinate system.

By default, the origin of a view's bounds is at 0, 0. This has nothing to do with the position of its frame. So, when you change the frame of a view that is at, say, 200, 0 (in its parent's co-ordinates) to be at 0, 0, you move it 200 points* to the left.

If you set the view's frame to its frame, the view will not move, as you expected.

(By the way: bounds and frame are both NSRects, so there's no reason to call NSMakeRect with the individual members of a rectangle if you're just going to pass them all through unchanged.)

*Not pixels. Co-ordinates of views are in points, an imaginary unit descended from the PostScript point (1/72 inch); you can't and shouldn't rely on points equaling pixels.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • Thanks, changing bounds to frame made it work as expected. I supposed I should have caught this earlier when it returned 0.00000 as the X and Y coordinates (I assumed the 0.000 was from other issues) – nosedive25 Nov 24 '11 at 02:34
1

bounds is expressed in terms of the view's local coordinate system. frame is expressed in the superview's coordinate space.

Firoze Lafeer
  • 17,133
  • 4
  • 54
  • 48
0

It looks like you are getting confused between the terms frame and bounds.

When you run that code, what is the value of your bounds origin? It is very likely {0,0} (check it in the debugger) because remember, the bounds deals with the view's own internal coordinates, whereas the frame deals with the superview's coordinates.

If your bounds origin is {0,0}, when you run your code you set the frame origin to {0,0}, also know as the top left corner of the superview. Your bounds origin still hasn't changed. So, the next time you run it,it will set the frame origin to {0,0}, which is where it already is, so no movement occurs.

If you change all of your bounds calls to frame, you'll get the original result you wanted, which was no movement even on the first call.

I would suggest learning more about the coordinate systems. Here are some places to start:

UIView Programming Guide Previous Answer on StackOverflow

Community
  • 1
  • 1
sosborn
  • 14,676
  • 2
  • 42
  • 46