0

In my program I'm drawing some shapes inside an NSView using NSBezierPath. Everything works and looks great except when I resize the window.

Example

Initial drawing

Initial drawing

Window resize Window resize

Does anyone know what I should use to prevent this square from being anchored to the initial position, but make it readjust relative the the initial scale.

Any help is appreciated!

Eugene Gordin
  • 4,047
  • 3
  • 47
  • 80
  • I'm drawing those shapes...they are not built in the interface builder – Eugene Gordin Apr 29 '13 at 18:00
  • yes i know, you can use autolayout by codes!!! – Anoop Vaidya Apr 29 '13 at 18:01
  • any example?!...I obviously have no idea what you're talking about :) – Eugene Gordin Apr 29 '13 at 18:02
  • Found [this](http://stackoverflow.com/questions/12593153/implementing-auto-layout-for-views-generated-programmatically) and [this](http://iphonedevsdk.com/forum/iphone-sdk-development/109628-programatically-change-autolayout-on-orientation-change.html) – Anoop Vaidya Apr 29 '13 at 18:04
  • thank you, I'll check those out! – Eugene Gordin Apr 29 '13 at 18:05
  • both of these links are for iOS...I'm doing it for OSX – Eugene Gordin Apr 29 '13 at 18:07
  • not much of difference in autolayout, codes.... see this one too http://developer.apple.com/library/mac/#documentation/UserExperience/Conceptual/AutolayoutPG/Articles/constraintFundamentals.html – Anoop Vaidya Apr 29 '13 at 18:08
  • @NSGod of course I'm not a native English speaking person...but I guess I know how to use articles in English – Eugene Gordin Apr 29 '13 at 21:58
  • @EugeneGordin: It should be "**an** `NSView`", not "a `NSView`". In English, you determine whether to use "a" or "an" not by looking directly at the letter that starts the next word, but by the sound made when that letter is spoken. If it sounds like a consonant, you use `a`, if it sounds like a vowel, you use `an`. *A* box, *an* egg, but also *a* user (because user is pronounced `yoo-zer`, which sounds like a consonant). `NSView` is pronounced `en-ess-VIEW`, which sounds like a vowel, hence using `an`. Your English is excellent, the edit wasn't meant as an attempt to insult you. ;-) – NSGod Apr 30 '13 at 00:04
  • Thank you for great explanation, I thought English is less complicated :))) – Eugene Gordin Apr 30 '13 at 05:03

1 Answers1

1

If you are doing your drawing in drawRect: then the answer is NO. You will need to rebuild and reposition your path each time. What you can do is something along the following lines:

- (void) drawRect:(NSRect)dirtyRect
{
    // Assuming that _relPos with x and y having values bewteen 0 and 1.
    // To keep the square in the middle of the view you would set _relPos to
    // CGPointMake(0.5, 0.5).

    CGRect bounds = self.bounds;

    CGRect rect;
    rect.size.width  = 100;
    rect.size.height = 100;
    rect.origin.x = bounds.origin.x + bounds.size.width  * _relPos.x - rect.size.width /2;
    rect.origin.y = bounds.origin.y + bounds.size.height * _relPos.y - rect.size.height/2;

    NSBezierPath *path = [NSBezierPath bezierPathWithRect:rect];
    [[NSColor redColor] set];
    path.lineWidth = 2; 
    [path stroke];
}
aLevelOfIndirection
  • 3,522
  • 14
  • 18
  • yeah...that's what I was thinking about this problem, that I'll have to write some algo to recalculate and redraw the shapes...thank you! – Eugene Gordin Apr 29 '13 at 22:04