Re: smooth resize
Re: smooth resize
- Subject: Re: smooth resize
- From: Oliver Cameron <email@hidden>
- Date: Sun, 13 Jun 2004 09:52:03 +0100
Hi,
I was at your stage once and I know what its like to just receive code
you have no idea what to do with.
So basically, in your class (your .m file), where you would like to
resize the window smoothly put this code (aka: method)
- (void)resizeTheWindow:(id)sender;
{
NSRect windowFrame;
NSRect newWindowFrame;
windowFrame = [myWindow frame];
newWindowFrame = NSMakeRect(NSMinX(windowFrame), NSMaxY(windowFrame),
500, 700);
[myWindow setFrame:newWindowFrame display:YES animate:YES];
}
Let me explain NSMakeRect, basically you need to satisfy 4 values to
make it work. Now the first too in the NSMakeRect bit you don't need to
mess with, its the last 2 bits (the 500,700). Now these 2 values say
how big (or small) you want your new window to be. 500 is the width,
700 the height.
Basically, in words this is how the method above works:
Your making 2 NSRect's (rectangles). You then tell the NSRect's what
they are, so in the code above you tell windowFrame that its the frame
of your current window. You then tell newWindowFrame about its new
size.
You then tell the window what frame you want it to be.
Now that the main bit is done, go into your header file (the same one
as above, only not .m but .h).
You need to define a few things first. So inside the { and the }, put:
IBOutlet NSWindow *myWindow;
And then underneath the } and before the @end, put:
- (void)resizeTheWindow:(id)sender;
Now that this is all working, you can put the method onto a button. To
do this, open your main nib in Interface Builder and drag across the .h
file onto your little nib window (the one with the tabs). You will see
it appear in a column view like way. Right click on it there, and
choose Instantiate Your Class Name. Switch back (via the tabs) to
instances and you should see a blue 3d box with your class name
underneath it. If you haven't already, drag in a window to the
instances tab. Now, If you press 'Control' and click and drag on your
keyboard, you should see a blue line appear. You need to Control and
Click on your new blue box, and drag it to the little window item in
the instances tab. When you've done so, connect the myWindow option.
Now, you want to actually resize it. So drag in a button or so, and
'Control' and click and drag again, but this time from the button to
the blue box. A list of methods should appear, choose the
resizeThisWindow: one. You can then save the application and build. And
voila, it should work.
If you have any other problems, don't hesitate to ask,
Thanks,
Oliver
On 13 Jun 2004, at 02:46, Rhon Fitzwater wrote:
First, thank you for the help and code example. I do have another
question though. Still being new to cocoa, I don't know how
everything works. How could I add this code so it works in my code
below. In the code, I have where I want it to do the resizing but am
not sure how to add the code you gave me so it works. Can you be of
any help?
Thanks,
-Rhon
//mouse entered exited window code below
- (void)viewDidMoveToWindow {
[self addTrackingRect:[self frame] owner:self userData:nil
assumeInside:YES];
[super viewDidMoveToWindow];
}
static float windowAlphaIncrement = 0.15;
static float windowAlphaCurrentIncrement;
static float windowAlphaMin = 0.2;
static float windowAlphaMax = 1.0;
static float windowAlphaFadeRate = 1.0 / 15.0;
- (void)changeTransparency:(NSTimer *)timer
{
float alpha = [[self window] alphaValue];
BOOL reschedule = NO;
alpha += windowAlphaCurrentIncrement;
// Reschedule timer if we haven't reached desired value
if (windowAlphaCurrentIncrement > 0)
{
if (alpha < windowAlphaMax)
reschedule = YES;
else
alpha = windowAlphaMax;
}
else
{
if (alpha > windowAlphaMin)
reschedule = YES;
else
alpha = windowAlphaMin;
}
if (reschedule)
[NSTimer scheduledTimerWithTimeInterval:windowAlphaFadeRate
target:self selector:@selector(changeTransparency:) userInfo:nil
repeats:NO];
[[self window] setAlphaValue:alpha];
}
- (void)mouseEntered:(NSEvent *)event {
//NSLog(@"Mouse Entered");
// Resize to expanded window should go here
windowAlphaCurrentIncrement = windowAlphaIncrement;
[NSTimer scheduledTimerWithTimeInterval:windowAlphaFadeRate
target:self selector:@selector(changeTransparency:) userInfo:nil
repeats:NO];
}
- (void)mouseExited:(NSEvent *)event {
//NSLog(@"Mouse Exited");
//Resize to small window code should go here
windowAlphaCurrentIncrement = -windowAlphaIncrement;
[NSTimer scheduledTimerWithTimeInterval:windowAlphaFadeRate
target:self selector:@selector(changeTransparency:) userInfo:nil
repeats:NO];
}
//end of mouse entered or exited window code
On Jun 12, 2004, at 3:40 PM, Oliver Cameron wrote:
Something like this would work:
- (void)resizeTheWindow:(id)sender;
{
NSRect windowFrame;
NSRect newWindowFrame;
windowFrame = [myWindow frame];
newWindowFrame = NSMakeRect(NSMinX(windowFrame), NSMaxY(windowFrame)
- titleToolbarHeight, NSWidth([myView frame]), titleToolbarHeight);
[myWindow setFrame:newWindowFrame display:YES animate:YES];
}
You should look up NSMakeRect to see the values you should use. For
example, I'm resizing to myView's size, you could use a hard-coded
value.
Hope this helps,
Oliver
On 12 Jun 2004, at 19:35, Rhon Fitzwater wrote:
Okay, so I got the smooth animation working thanks to Daniel
Waylonis.
But now I know how to do the smooth resize. I have some code that I
used in one of my AS Studio programs:
- (void)setFrame:(NSRect)frameRect display:(BOOL)displayFlag
animate:(BOOL)animateFlag;
Using call method:
call method "setFrame:display:animate:" of object window of theObject
with parameters {{259, 256, 300, 400}, true, true}
How would I convert this into just cocoa code for my cocoa program?
Thanks,
-Rhon
[demime 0.98b removed an attachment of type
application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.