How to Animate a custom NSWindow - objective-c

I'm having some troubles animating a custom NSWindow.
Here's my init method:
- (CustomWindow *)initWithView:(NSView *)view
{
if ((self = [super initWithContentRect:contentRect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO])) {
[[self contentView] addSubview:view];
[self setBackgroundColor:[NSColor darkGrayColor]];
[self setMovableByWindowBackground:YES];
[self setExcludedFromWindowsMenu:YES];
[self setOpaque:NO];
[self setHasShadow:YES];
[self setDelegate:self];
}
return self;
}
When I call [self setFrame:originalFrame display:NO animate:YES]; there is a delay that corresponds to the animation duration, but the animation itself doesn't occur.
I think this is because I'm using a borderless window?

Simply use this: [self setFrame:originalFrame display:YES animate:YES];

Guess your window is resized, but view you add to content view, don't. If you have only one view, then instead of:
[[self contentView] addSubview:view];
use:
[self setContentView:view];

Related

Cocoa NSAnimationContex Weird behaviour

I have an NSView NSButton and an NSTextField in this view's subviews
all of them are Layer Backed
I ma trying to animate this view changing frame.
- (void)animateTheView:(SPTabView *)view toFrame:(NSRect)endFrame
{
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context)
{
context.allowsImplicitAnimation = YES;
context.duration = ANIMATION_TIME;//is a define 0.6f
[view setFrame:endFrame];
} completionHandler:nil];
}
Edit In my view I am not using AutoLayout I am just overriding the setFrame:
- (void)setFrame:(NSRect)frame
{
[super setFrame:frame];
[_titleLabel setFrame:self.bounds]; //The NSTextField I am talking about
[_closeButton setFrame:NSMakeRect(5, 2, B_WIDTH, B_HEIGHT)];//This one works fine
[self removeTrackingRect:_tracking];
_tracking = [self addTrackingRect:[self bounds] owner:self userData:NULL assumeInside:NO];
}
EDIT 2 Posting the subViews Initialisation Code
[super setWantsLayer:YES];
[self setWantsLayer:YES];
[self setCanDrawSubviewsIntoLayer:YES];
[self drawRegular];
_closeButton = [[NSButton alloc]init];
[_closeButton setCanDrawSubviewsIntoLayer:YES];
[_closeButton setTitle:#"X"];
[_closeButton setTarget:self];
[_closeButton setAction:#selector(closeTab)];
[_closeButton setHidden:YES];
[_closeButton setWantsLayer:YES];
_titleLabel = [[SPLabel alloc]init]; //SPLabel is a silly subclass of NSTextField which just set it's properties like editable to as NO
[_titleLabel setText:#"Untitled.txt"];
[_titleLabel setTextColor:[NSColor blackColor]];
[_titleLabel setAlignment:NSCenterTextAlignment];
[_titleLabel setWantsLayer:YES];
[_titleLabel setTag:6];
[self addSubview:_titleLabel];
[self addSubview:_closeButton];
My view and it's button animating FINE! but the textField doesn't animate at all - it just jumps to the EndFrame...
Any Ideas some1 ?

Objective C, rounded corner custom window?

I have a subclass of NSWindow to customize one of my windows for my app.
I have everything set, but I am not sure how to make the corners round.
Currently, my window is a transparent rectangular window with some buttons, labels, and a textfield in it.
The class includes:
#import "TransparentRoundRectWindow.h"
#implementation TransparentRoundRectWindow
-(id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag
{
self = [super initWithContentRect:contentRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO];
if (self) {
[self setAlphaValue:0.75];
[self setOpaque:YES];
[self setHasShadow:YES];
[self setBackgroundColor:[NSColor clearColor]];
}
return self;
}
-(BOOL)canBecomeKeyWindow
{
return YES;
}
I just need to make the corners round now. I tried searching for similar situations and saw some of them explaining to override the drawRect method but I couldn't get them to work.
How could I do this?
(I'm using Mac OS X Lion)
Thanks in advance.
You need to set to Your window Opaque to NO. And subclass Your window's view.
Window subclass:
-(id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag
{
self = [super initWithContentRect:contentRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO];
if (self) {
[self setOpaque:NO];
[self setHasShadow:YES];
[self setBackgroundColor:[NSColor clearColor]];
}
return self;
}
-(BOOL)canBecomeKeyWindow
{
return YES;
}
Window's view subclass:
- (void)drawRect:(NSRect)rect
{
NSBezierPath * path;
path = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:8 yRadius:8];
[[NSColor colorWithCalibratedRed:0 green:0 blue:0 alpha:0.75] set];
[path fill];
}
Result:
More explanation how to do this:
Create new NSView class and paste "Window's view subclass" code which I wrote in it. Then go to Your window's view.
Here is window's view click on it:
Go to the Identity inspector and set class to your created class:

View Won't Accept First Responder Status

I'm creating a new window and view programmatically. I need the view to accept First Responder status. For whatever reason it's not.
I'm doing this old school because I want my app to be compatible with older versions of OSX. Here's what I've got:
- (IBAction)startShow:(id)sender {
// Capture the main display
if (CGDisplayCapture( kCGDirectMainDisplay ) != kCGErrorSuccess) {
NSLog( #"Couldn't capture the main display!" );
}
// Get the shielding window level
windowLevel = CGShieldingWindowLevel();
// Get the screen rect of our main display
screenRect = [[NSScreen mainScreen] frame];
// Put up a new window
displayWindow = [[ShowWindow alloc] initWithContentRect:screenRect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO screen:[NSScreen mainScreen]];
[displayWindow setBackgroundColor:[NSColor blackColor]];
[displayWindow setLevel:windowLevel];
[displayWindow makeKeyAndOrderFront:nil];
// Load show window with options from settings window.
[displayWindow loadOptions:screenRect];
}
Just to clarify, displayWindow is an instance of ShowWindow, which is a subclass of NSWindow. In my ShowWindow class I'm doing the following:
- (BOOL)canBecomeKeyWindow
{
return YES;
}
- (void) loadOptions:(NSRect)screenRect {
// Create custom view to hold text fields
view = [[ShowView alloc] initWithFrame: screenRect];
[self setContentView:view];
// Track Title Text Field Initialize
textField = [[NSTextField alloc] initWithFrame:NSMakeRect(0, screenRect.size.height / 4, screenRect.size.width, 350)];
[textField setStringValue:#"My Label"];
[textField setBezeled:NO];
[textField setDrawsBackground:NO];
[textField setEditable:NO];
[textField setSelectable:NO];
[textField setTextColor:[NSColor whiteColor]];
[textField setFont:[NSFont fontWithName:#"Helvetica" size:92]];
[textField setAlignment:NSCenterTextAlignment];
// Add track title text field to main view.
[view addSubview:textField];
// Make the view first responder
[self makeFirstResponder:view];
}
view is an instance of ShowView, which is a subclass of NSView. Last but not least here's what I've got going on in ShowView:
// Next four methods set main view as first responder to accept keyboard input
- (BOOL)acceptsFirstResponder
{
NSLog(#"I accepted being a first responder! Yea!");
return YES;
}
- (BOOL)resignFirstResponder
{
[self setNeedsDisplay:YES];
return YES;
}
- (BOOL)becomeFirstResponder
{
[self setNeedsDisplay:YES];
return YES;
}
- (void)insertText:(NSString *)input
{
}

NSWindow like Quicksilver

I am creating an app, where I want to show a "pop-up" or "notification" to the user when he press a hotkey.
I want that to be displayed as a floating message, like the one that appears when you change the volume, or the brightness, or like the Quicksilver App input window.
I want it to has alpha, to fade in, to stay there for 5-8 secs, and to fade out.
And ... the problem is that I don't know how to get started on that. I've tried:
NSRect frame = NSMakeRect(0, 0, 600, 65);
_myWindow = [[NSWindow alloc] initWithContentRect:frame
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO];
[_myWindow setBackgroundColor:[NSColor blueColor]];
[_myWindow center];
[_myWindow makeKeyAndOrderFront:NSApp];
But, of course that's not what I'm trying to do.
Any help is welcome ;-)
I believe the proper term for this is a bezel window. Quicksilver is open source (https://github.com/quicksilver/Quicksilver) so you can see how they do it, but I've accomplished something similar with a custom NSWindow subclass:
- (id) initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag
{
if((self = [super initWithContentRect:contentRect styleMask:NSBorderlessWindowMask backing:bufferingType defer:flag])) {
[self setOpaque:NO];
[self setIgnoresMouseEvents:YES];
[self setLevel:NSFloatingWindowLevel];
[self setBackgroundColor:[NSColor clearColor]];
}
return self;
}
and the animation controlled by the window controller:
- (void) windowDidLoad
{
CAAnimation *animation = [CABasicAnimation animation];
[animation setDelegate:self];
[[self window] setAnimations:[NSDictionary dictionaryWithObject:animation forKey:#"alphaValue"]];
}
to make the window fade in, code similar to the following should work:
[[self window] setAlphaValue:0.0];
[[[self window] animator] setAlphaValue:1.0];
[[self window] orderFront:self];
I realize this isn't a plug and play solution but it should get you started.

Cocoa App Enter Full Screen

I am developing a simple application which make use of full screen window.
Window contains view that contains button, image etc etc... , but when I enter in full screen with the follow lines:
NSWindow* tmp = [self window];
[tmp setStyleMask:NSBorderlessWindowMask];
[tmp setFrame:[tmp frameRectForContentRect:[[tmp screen] frame]]display:YES animate:NO];
[tmp setBackingType:NSBackingStoreBuffered];
screenRect = [[NSScreen mainScreen] frame];
int windowLevel = CGShieldingWindowLevel();
[self.window setLevel:windowLevel];
The view I put in the window doesn't resized automatically, I could make some operation for resize correctly that work, but there are a way to do that automatically?
I post all my AppController here:
-(id)init {
self = [super initWithWindowNibName:#"MainWindow"];
NSWindow* tmp = [self window];
[tmp setStyleMask:NSBorderlessWindowMask];
[tmp setFrame:[tmp frameRectForContentRect:[[tmp screen] frame]]display:YES animate:NO];
[tmp setBackingType:NSBackingStoreBuffered];
screenRect = [[NSScreen mainScreen] frame];
/**
// [[tmp standardWindowButton:NSWindowMiniaturizeButton] setHidden:YES];
// [[tmp standardWindowButton:NSWindowZoomButton] setHidden:YES];
self.window = [[NSWindow alloc] initWithContentRect:screenRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO screen:[NSScreen mainScreen]];
**/
int windowLevel = CGShieldingWindowLevel();
[self.window setLevel:windowLevel];
return self;
}
// We need to be layer-backed to have subview transitions.
-(void)awakeFromNib {
[[self window] setContentSize:[topMenu frame].size];
[[[self window] contentView] addSubview:topMenu];
[topMenu enterFullScreenMode:[NSScreen mainScreen] withOptions:nil];
[[[self window] contentView] setWantsLayer:YES];
}
- (void)dealloc
{
[super dealloc];
}
- (void)windowDidLoad
{
[super windowDidLoad];
// Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
[topMenu_controller performAnimation];
return;
}
You can use the springs and struts of Interface Builder to set a view's autosizing behavior:
http://developer.apple.com/library/mac/#documentation/DeveloperTools/Conceptual/IB_UserGuide/Layout/Layout.html