Animate setContent of an NSWindow - objective-c

In the past I've been successfully able to fade in an NSWindow using the following code;
if (![statusWindow isVisible])
{
statusWindow.alphaValue = 0.0;
[statusWindow.animator setAlphaValue:1.0];
}
CAAnimation *anim = [CABasicAnimation animation];
[anim setDelegate:self];
[statusWindow setAnimations:[NSDictionary dictionaryWithObject:anim forKey:#"alphaValue"]];
[statusWindow makeKeyAndOrderFront:self];
For my current project I'm trying to make a flash similar to the one in Photo Booth. I've created a white NSPanel and was planning to set my NSWindow's content to the panel, and quickly set it back.
Is it possible to set the contentView of an NSWindow using a nice fade effect?
P.S - If there is an easier way you know of how to achieve the flash, please tell me!
Thanks in advance,
Ricky.

Why use another window? It looks like you're trying to use CoreAnimation already so why not just add a white CALayer to your existing view and animate its opacity?

Related

CATransition push without fade

I'm making a navigation controller class for Mac OS X.
I want to replace the current view with a kCATransitionPush animation.
Like in this post:
Core Animation Tutorial - Wizard Dialog With Transitions
The CATransition is set up like this:
CATransition *transition = [CATransition animation];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromLeft;
[self.view setAnimations:#{ #"subviews" : transition }];
However, when I replace the views, there is a fade animation, which is being automatically added.
[NSAnimationContext beginGrouping];
{
[[self.view animator] replaceSubview:_currentViewController.view with:newViewController.view];
}
[NSAnimationContext endGrouping];
How can I do a push animation without the fading?
The best way I've found to get smooth Core Animation transitions that works regardless of whether the view supports CA or not is to do the following:
Create an image of the view you are trying to animate, using NSView -cacheDisplayInRect:toBitmapImageRep or a similar method
Put that image in an NSImageView
Layer back the image view, which will draw fine without glitches when layer backed
Add the image view as a subview over the view that you are trying to transition
Animate the image view frame using NSView's animator proxy
Remove the image view once the animation has completed
I suspect you're running into implicit animations - Core Animation will automatically animate layer property changes that happen outside of your own transactions.
There's a good summary of several methods for disabling these implicit animations in these two questions:
How to disable CALayer implicit animations?
Disabling implicit animations in -[CALayer setNeedsDisplayInRect:]
...and you can read more about implicit transactions in the Core Animation docs
I think the transition from left transition includes a built-in fade. The IOS push transitions do.
If you don't want that, you might have to roll your own push transition using Core Animation. This would be easy in iOS with UIView animations. Sadly, there is not an equivalent in Mac OS. I wish Apple would go back and add view animations to Mac OS. I get spoiled using them in iOS, and then miss them when I work on Mac applications.
Add below code
CATransition *transition = [CATransition animation];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromLeft;
[transition setDuration:0.5];
[self.view.layer addAnimation:transition forKey:kCATransition];

setImage animated - NSImageView

I'm trying to setImage: of a NSImageView using animation (Mac App):
[[_image animator] setImage:[NSImage imageNamed:#"image.png"]];
But the image is just changing (without some effect). I'm trying to fade it.
Any idea how to fade two NSImageView in a Mac App?
I think this link might help you with transitions and animations with NSImageView :
http://developer.apple.com/library/mac/samplecode/ImageTransition/ImageTransition.zip
Hope this helps

Is there Any way to imitate Lion's Launchpad?

I have been struggling to imitate Launchpad.
At the beginning I thought about making NSWindow bgcolor transparent:
//make NSWindow's bgcolor transparent
[window setOpaque:NO];
[window setBackgroundColor:[NSColor clearColor]];
But now I realized it's way more ideal to
capture wallpaper
blur it and make it bg-image for NSWindow or a view
Rather than hiding all the opened windows and menubar, which was the first idea I had have come with (Still not sure with better, if you had any better idea...).
Capture & blur wallpaper used by a user
Make it background image for nswindow or a view
Fade-in to fullscreen view
Click somewhere blank or press ESC to fade-out
Are those possible to achieve without using private APIs?
Sorry if it's not clear my poor English.
Simply I'm trying to imitate Launchpad-styled full screen.
Thanks for any advice.
To get an image of the desktop background, use:
NSURL *imageURL = [[NSWorkspace sharedWorkspace] desktopImageuRLForScreen:[NSScreen mainScreen]
NSImage *theDekstopImage = [[NSImage alloc] initWithContentsOfURL:imageURL];
You can blur the image using CIFilter. Here's a Apple doc describing how to apply filters.
And then you can load that image into a color and set that as the background color for the window. Additionally, set the window to have no style mask (no close buttons, title frame, etc.), cover the screen, and be in front of everything except the dock:
[yourWindow setBackgroundColor:[NSColor colorWithPatternImage:theDesktopImage]];
[yourWindow setStyleMask:NSBorderlessWindowMask];
[yourWindow setLevel:kCGDockWindowLevel - 1];
[yourWindow setFrame:[[NSScreen mainScreen] frame] display:YES];
To have the window fade in, you can use NSWindow's animator proxy. (Replace 1.0 with 0.0 to make it fade out.)
[[yourWindow animator] setAlphaValue:1.0];
Of course you could customize that a bit more with things like CoreAnimation, but this should work just fine.
To handle background clicking, I suggest making a subclass of NSView where you -orderOut: your window on -mouseDown:. Then put an instance of that subclass that spans the entire frame of your window.
Also, NSViews sometimes don't respond to key events, so you can add an event listener to detect any time the escape key is pressed while your app is active:
[NSEvent addLocalMonitorForEventsMatchingMask:NSKeyDownMask handler:(NSEvent *ev)^ {
if([ev keyCode] == 0x53) {
[yourWindow orderOut:self];
}
return ev;
}

Touch event of a picture without using UIButton

Is it possible to register a touch of a picture without using UIButton? And if it is, how can i do this?
I can just think of changing the background of a button in the code, and then use IBAction to detect the touch.
Btw, sorry for my english.
Sure. You can put a UITapGestureRecognizer on a UIImageView (remember to set the image view's userInteractionEnabled property to YES):
// assumes "myImageView" is your UIImageView that has user interaction enabled
UITapGestureRecognizer * tap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tap:)] autorelease];
[myImageView addGestureRecognizer:tap];
later in your controller...
- (void)tap:(UITapGestureRecognizer *)recognizer
{
// whatever you want
}
However, you can set up a UIButton to basically be a dumb image. Just set the background image for the normal state, disable all the adjusting on highlighting and touches, and you should have exactly what you're thinking of.
If I've misunderstood what you're getting at, feel free to clarify.

Animating the UIButton Layer?

I'm trying to bring the ripple effect seen in the dashboard application to iphone. My idea is whenever i place a button in the layout view there should be ripple effect around the button.
However, I`m able to bring the ripple effect for the whole view but I needs the effect only around the button. I don know where I went wrong. I tried the following code.
LayoutButton *tempLayoutButton=[[LayoutButton alloc] initWithObject:object];
tempLayoutButton.center=copyImage.center;
[layoutView addSubview:tempLayoutButton];
CALayer *templayer=[CALayer layer];
tempLayoutButton.layer.masksToBounds=NO;
templayer.frame=CGRectMake(0,0,50,50);
[tempLayoutButton.layer addSublayer:templayer];
CATransition *animation = [CATransition animation];
animation.delegate = self;
animation.duration = 1.0f;
animation.timingFunction = UIViewAnimationCurveEaseInOut;
animation.type = #"rippleEffect";
[templayer addAnimation:animation forKey:#"animation"];
[tempLayoutButton release];
[tempLayoutButton.layer addSublayer:templayer];
If I replace templayer with LayoutView layer I can see the riple effect from the whole view. Can anyone tell me the solution
Thanks in advance
I found the answer in one of apples mailing list. The animation is apple`s own animation and no one can use that. And there is no guarantee for the animation to appear properly in the screen. If we need the same type of animation we need to manipulate the layers and get the animation