Search code examples
objective-cnsviewcgrect

Move rect in NSView


I've got a NSView subclass "graphics" where some rects are drawn.

Now I want a public method to move/resize the rects. I tried to set a new origins, but that doesn't work for me.

rect.origin.x = anything;

Now how can I move the rects?

regards


Solution

  • You are drawing the rectangles in a method called "drawRect", which is run every time the NSView thinks it should need to redisplay itself.

    Your problem is that you are setting the initial location of the rectangles in this method, so every changed you make in other method wouldn't matter and would be reset every time. You should instead set the rectangles initial location in methods like init or awakeFromNib that only happen once.

    After your button changed the values of the origin of the rectangle, you can tell the view to redraw itself, using :

    [self setNeedsDisplay:YES];
    

    So add this after the code that changes the rect origin and you should be fine.