Here is my code snippet for cocoa application using core animation, somehow the animation doesn't show.
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:#"position"];
[animation setDelegate:self];
NSRect pos = [imageView frame];
[animation setFromValue:[NSValue valueWithRect:pos]];
NSPoint point = NSMakePoint(pos.origin.x-40, pos.origin.y);
[animation setToValue:[NSValue valueWithPoint:point]];
[animation setDuration:2.0];
[[imageView animator] addAnimation:animation forKey:#"myTest"];
while this is the working code:
NSRect position = [imageView frame];
position.origin.x -= 40;
[[imageView animator] setFrame:position];
But autoReverse doesn't work.
Anything wrong with the first one? And how to make the reverse movement work in the 2nd one? Thanks!
I don't know for sure, but for the first one, position is a CGPoint, so you might try using that type for your fromValue and toValue. You're currently using an NSRect and an NSPoint (the latter of which should work, but not sure about the former).
For the second, how are you specifying auto-reverse? +setAnimationRepeatAutoreverses needs to be called from inside an animation block (after "beginAnimations")
(Note: I'm not very experienced with Cocoa since I'm chiefly an iOS developer; I haven't tested any of the following stuff)
I think the problem is that you're trying to mix CoreAnimation and Animator Proxy. You don't add the animation to the Animator, but to the layer:
[[imageView layer] addAnimation:animation forKey:#"myTest"];
Another possibility might be to use NSViewAnimation and chain them together. See the Animation Programming Guide for Cocoa, page 13. So you'd have one animation to go in one direction, and once it's finished it triggers the second one that goes back. It seems to work like this:
NSMutableDictionary *firstDict = [NSMutableDictionary dictionary];
[firstDict setObject:imageView forKey:NSViewAnimationTargetKey];
[firstDict setObject:[NSValue valueWithRect:originalFrame] forKey:NSViewAnimationStartFrameKey];
[firstDict setObject:[NSValue valueWithRect:targetFrame] forKey:NSViewAnimationEndFrameKey];
NSMutableDictionary *secondDict = [NSMutableDictionary dictionary];
[secondDict setObject:imageView forKey:NSViewAnimationTargetKey];
[secondDict setObject:[NSValue valueWithRect:targetFrame] forKey:NSViewAnimationStartFrameKey];
[secondDict setObject:[NSValue valueWithRect:originalFrame] forKey:NSViewAnimationEndFrameKey];
NSViewAnimation *firstAnimation = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:firstDict]];
[firstAnimation setDuration:2.0];
NSViewAnimation *secondAnimation = [[NSViewAnimation alloc] initWithViewAnimations:[NSArray arrayWithObject:secondDict]];
[secondAnimation setDuration:2.0];
[secondAnimation startWhenAnimation:firstAnimation reachesProgress:1.0];
[firstAnimation startAnimation];
Then, in Lion (OS X 10.7) you can set a completion handler when using Animator Proxy. It should work like this:
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:2.0];
[[NSAnimationContext currentContext] setCompletionHandler:^(void) {
// Here comes your code for the reverse animation.
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:2.0];
[[aView animator] setFrameOrigin:originalPosition];
[NSAnimationContext endGrouping];
}];
[[aView animator] setFrameOrigin:position];
[NSAnimationContext endGrouping];
Related
My NSProgressIndicator is animating in a view
when I switch to another view ,and switch back
my NSProgressIndicator disappear.
why?
Here is my code for switch to another view
- (void)switchToView:(NSView *)newView withAnimate:(BOOL)animate
{
if ([[rootView subviews] count] != 0)
{
[rootView setWantsLayer:YES];
[rootView displayIfNeeded];
NSTimeInterval duration = [[self window] animationResizeTime:newFrame];
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:0.25];
[[rootView animator] replaceSubview:[[rootView subviews] objectAtIndex:0] with:newView];
[NSAnimationContext beginGrouping];
if (duration > 0.25) {
[[NSAnimationContext currentContext] setDuration:0.25];
}
else{
[[NSAnimationContext currentContext] setDuration:duration];
}
[[[self window] animator] setFrame:newFrame display:YES animate:YES];
[NSAnimationContext endGrouping];
[NSAnimationContext endGrouping];
[self performSelector:#selector(endAnimation) withObject:nil afterDelay:0.25f];
}
}
-(void)endAnimation{
[[ _mainWindow contentView] setWantsLayer:NO];
}
I just replace a view,and make a fade out animation NSProgressIndicator animating all the time in a view.
I have solve this by myself ,I think it will be helpful for your guys.
I stop my NSProgressIndicator before I want to replace my view with NSProgressIndicator.
then I replace the view with a new view ,if I switch back I will start NSProgressIndicator with a - (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay; method ,just set the delay=0 is OK.
In xib files , you should set NSProgressIndicator DisplayWhenStopped is YES.
And it will work well.
Then reason is after 10.6.5,At least on OS X 10.6.5 and above, as soon as you set an indetermined-progress-indicator's wantsLayer property to YES, the animation stops immediately .
So before it stop immediately ,I stop it by myself.And start it when I want to use it.
NOTICE: You should stop it by yourself better than system does, it can`t work well if system stop it .
I'm working at a custom Window object that is displayed as child in a parent Window.
For this object I'd like to create an animation like that of an NSPopover.
My first idea is to create a Screenshot of the child Window, than animate it using Core Animation and finally showing the real Window.
Before begging the implementation I would like to know if exists a better method and what you think about my solution.
It's not trivial. Here's how I do it:
#interface ZoomWindow : NSWindow
{
CGFloat animationTimeMultiplier;
}
#property (nonatomic, readwrite, assign) CGFloat animationTimeMultiplier;
#end
#implementation ZoomWindow
#synthesize animationTimeMultiplier;
- (NSTimeInterval)animationResizeTime: (NSRect)newWindowFrame
{
float multiplier = animationTimeMultiplier;
if (([[NSApp currentEvent] modifierFlags] & NSShiftKeyMask) != 0) {
multiplier *= 10;
}
return [super animationResizeTime: newWindowFrame] * multiplier;
}
#end
#implementation NSWindow (PecuniaAdditions)
- (ZoomWindow*)createZoomWindowWithRect: (NSRect)rect
{
// Code mostly from http://www.noodlesoft.com/blog/2007/06/30/animation-in-the-time-of-tiger-part-1/
// Copyright 2007 Noodlesoft, L.L.C.. All rights reserved.
// The code is provided under the MIT license.
// The code has been extended to support layer-backed views. However, only the top view is
// considered here. The code might not produce the desired output if only a subview has its layer
// set. So better set it on the top view (which should cover most cases).
NSImageView *imageView;
NSImage *image;
NSRect frame;
BOOL isOneShot;
frame = [self frame];
isOneShot = [self isOneShot];
if (isOneShot) {
[self setOneShot: NO];
}
BOOL hasLayer = [[self contentView] wantsLayer];
if ([self windowNumber] <= 0) // <= 0 if hidden
{
// We need to temporarily switch off the backing layer of the content view or we get
// context errors on the second or following runs of this code.
[[self contentView] setWantsLayer: NO];
// Force window device. Kinda crufty but I don't see a visible flash
// when doing this. May be a timing thing wrt the vertical refresh.
[self orderBack: self];
[self orderOut: self];
[[self contentView] setWantsLayer: hasLayer];
}
// Capture the window into an off-screen bitmap.
image = [[NSImage alloc] initWithSize: frame.size];
[[self contentView] lockFocus];
NSBitmapImageRep* rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect: NSMakeRect(0.0, 0.0, frame.size.width, frame.size.height)];
[[self contentView] unlockFocus];
[image addRepresentation: rep];
// If the content view is layer-backed the above initWithFocusedViewRect call won't get the content
// of the view (seems it doesn't work for CALayers). So we need a second call that captures the
// CALayer content and copies it over the captured image (compositing so the window frame and its content).
if (hasLayer)
{
NSRect contentFrame = [[self contentView] bounds];
int bitmapBytesPerRow = 4 * contentFrame.size.width;
CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
CGContextRef context = CGBitmapContextCreate (NULL,
contentFrame.size.width,
contentFrame.size.height,
8,
bitmapBytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colorSpace);
[[[self contentView] layer] renderInContext: context];
CGImageRef img = CGBitmapContextCreateImage(context);
CFRelease(context);
NSImage *subImage = [[NSImage alloc] initWithCGImage: img size: contentFrame.size];
CFRelease(img);
[image lockFocus];
[subImage drawAtPoint: NSMakePoint(0, 0)
fromRect: NSMakeRect(0, 0, contentFrame.size.width, contentFrame.size.height)
operation: NSCompositeCopy
fraction: 1];
[image unlockFocus];
}
ZoomWindow *zoomWindow = [[ZoomWindow alloc] initWithContentRect: rect
styleMask: NSBorderlessWindowMask
backing: NSBackingStoreBuffered
defer: NO];
zoomWindow.animationTimeMultiplier = 0.3;
[zoomWindow setBackgroundColor: [NSColor colorWithDeviceWhite: 0.0 alpha: 0.0]];
[zoomWindow setHasShadow: [self hasShadow]];
[zoomWindow setLevel: [self level]];
[zoomWindow setOpaque: NO];
[zoomWindow setReleasedWhenClosed: NO];
[zoomWindow useOptimizedDrawing: YES];
imageView = [[NSImageView alloc] initWithFrame: [zoomWindow contentRectForFrameRect: frame]];
[imageView setImage: image];
[imageView setImageFrameStyle: NSImageFrameNone];
[imageView setImageScaling: NSScaleToFit];
[imageView setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
[zoomWindow setContentView: imageView];
[self setOneShot: isOneShot];
return zoomWindow;
}
- (void)fadeIn
{
[self setAlphaValue: 0.f];
[self orderFront: nil];
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration: 0.3];
[[self animator] setAlphaValue: 1.f];
[NSAnimationContext endGrouping];
}
- (void)zoomInWithOvershot: (NSRect)overshotFrame withFade: (BOOL)fade makeKey: (BOOL)makeKey
{
[self setAlphaValue: 0];
NSRect frame = [self frame];
ZoomWindow *zoomWindow = [self createZoomWindowWithRect: frame];
zoomWindow.alphaValue = 0;
[zoomWindow orderFront: self];
NSDictionary *windowResize = #{NSViewAnimationTargetKey: zoomWindow,
NSViewAnimationEndFrameKey: [NSValue valueWithRect: overshotFrame],
NSViewAnimationEffectKey: NSViewAnimationFadeInEffect};
NSArray *animations = #[windowResize];
NSViewAnimation *animation = [[NSViewAnimation alloc] initWithViewAnimations: animations];
[animation setAnimationBlockingMode: NSAnimationBlocking];
[animation setAnimationCurve: NSAnimationEaseIn];
[animation setDuration: 0.2];
[animation startAnimation];
zoomWindow.animationTimeMultiplier = 0.5;
[zoomWindow setFrame: frame display: YES animate: YES];
[self setAlphaValue: 1];
if (makeKey) {
[self makeKeyAndOrderFront: self];
} else {
[self orderFront: self];
}
[zoomWindow close];
}
This is implemented in an NSWindow category. So you can call:
- (void)zoomInWithOvershot: (NSRect)overshotFrame withFade: (BOOL)fade makeKey: (BOOL)makeKey
on any NSWindow.
I should add that I haven't been able to get both animations to run at the same time (fade and size), but the effect is quite similar to how NSPopover does it. Maybe someone else can fix the animation issue.
Needless to say this code works on 10.6 too where you don't have NSPopover (that's why I have written it in the first place).
I have an MAAttachedWindow (subclass of NSWindow), which contains a blank view (contentView), and some arbitrary subview (right now an NSImageView).
On load, I'm attempting to resize the window by 100px vertically, in an animated fashion.
This code block initializes my window and views:
_popoverContentView = [[NSView alloc] initWithFrame: aFrame];
NSImageView *img = [[NSImageView alloc] initWithFrame: aFrame];
[img setImage: [NSImage imageName: "#my_debug_image"]];
[img setAutoresizingMask: NSViewHeighSizable];
[_popoverContentView setAutoresizesSubviews: YES];
[_popoverContentView addSubview: img];
popover = [[MAAttachedWindow alloc] initWithView: _popoverContentView attachedToPoint: aPoint inWindow: nil onSide: MAPositionBottom atDistance: aDist];
And this code block is responsible for the animation:
NSRect newPopoverFrame = popover.frame;
NSRect newPopoverContentViewFrame = _popoverContentView.frame;
newPopoverFrame.size.height += 100;
newPopoverContentViewFrame.size.height += 100;
[_popoverContentView animator] setFrame: newPopoverContentViewFrame];
[[popover animator] setFrame: newPopoverFrame display: YES animate: YES];
Now this all works (almost) as expect, however as shown in this video, the animation is unreliable, shaky, and jumpy. I can't seem to pinpoint what in my code is causing this, or how to go about locking the image view into place.
I think the problem is that you're using the new(ish) animator proxy to animate the window's frame while also using the much older animate: parameter of NSWindow's setFrame:display:animate:, which uses the old NSViewAnimation API.
These two animation methods are probably conflicting as they try to animate the window simultaneously using different code paths.
You also need to wrap multiple calls to the animator proxy in [NSAnimationContext beginGrouping] and [NSAnimationContext endGrouping] if you wish the animations to be simultaneous.
Try doing this:
[NSAnimationContext beginGrouping];
[_popoverContentView animator] setFrame: newPopoverContentViewFrame];
[[popover animator] setFrame: newPopoverFrame display: YES animate:NO];
[NSAnimationContext endGrouping];
If that doesn't work, you could drop the use of the problematic setFrame:display:animate: method and just animate the position and size independently:
[NSAnimationContext beginGrouping];
[_popoverContentView animator] setFrame: newPopoverContentViewFrame];
[[popover animator] setFrameOrigin: newPopoverFrame.origin];
[[popover animator] setFrameSize: newPopoverFrame.size];
[NSAnimationContext endGrouping];
The animation context grouping will ensure that everything happens simultaneously.
I want to switch the image shown in an NSImageView, but I want to animate that change. I've tried various methods to do this. Hopefully one of you could suggest one that might actually work. I'm working with Cocoa for Mac.
As far as I know, NSImageView doesn't support animating image changes. However, you can place a second NSImageView on top of the first one and animate hiding the old one and showing the new one. For example:
NSImageView *newImageView = [[NSImageView alloc] initWithFrame: [imageView frame]];
[newImageView setImageFrameStyle: [imageView imageFrameStyle]];
// anything else you need to copy properties from the old image view
// ...or unarchive it from a nib
[newImageView setImage: [NSImage imageNamed: #"NSAdvanced"]];
[[imageView superview] addSubview: newImageView
positioned: NSWindowAbove relativeTo: imageView];
[newImageView release];
NSDictionary *fadeIn = [NSDictionary dictionaryWithObjectsAndKeys:
newImageView, NSViewAnimationTargetKey,
NSViewAnimationFadeInEffect, NSViewAnimationEffectKey,
nil];
NSDictionary *fadeOut = [NSDictionary dictionaryWithObjectsAndKeys:
imageView, NSViewAnimationTargetKey,
NSViewAnimationFadeOutEffect, NSViewAnimationEffectKey,
nil];
NSViewAnimation *animation = [[NSViewAnimation alloc] initWithViewAnimations:
[NSArray arrayWithObjects: fadeOut, fadeIn, nil]];
[animation setAnimationBlockingMode: NSAnimationBlocking];
[animation setDuration: 2.0];
[animation setAnimationCurve: NSAnimationEaseInOut];
[animation startAnimation];
[imageView removeFromSuperview];
imageView = newImageView;
[animation release];
If your view is big and you can require 10.5+, then you could do the same thing with Core Animation, which will be hardware accelerated and use a lot less CPU.
After creating newImageView, do something like:
[newImageView setAlphaValue: 0];
[newImageView setWantsLayer: YES];
// ...
[self performSelector: #selector(animateNewImageView:) withObject: newImageView afterDelay: 0];
- (void)animateNewImageView:(NSImageView *)newImageView;
{
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration: 2];
[[newImageView animator] setAlphaValue: 1];
[[imageView animator] setAlphaValue: 0];
[NSAnimationContext endGrouping];
}
You'll need to modify the above to be abortable, but I'm not going to write all your code for you :-)
You could implement your own custom view that uses a Core Animation CALayer to store the image. When you set the contents property of the layer, the image will automatically smoothly animate from the old image to the new one.
- (void)mouseDragged:(NSEvent *)theEvent {
NSSize dynamicImageSize;
dynamicImageSize = [[self image] size];
NSSize contentSize = [(NSScrollView*)[[self superview] superview] contentSize];
if(dynamicImageSize.height > contentSize.height || dynamicImageSize.width > contentSize.width)
{
float x = startOrigin.x - ([theEvent locationInWindow].x - startPt.x);
float y = startOrigin.y - ([theEvent locationInWindow].y - startPt.y);
[self scrollPoint:NSMakePoint(x, y)];
}
}
In the above code I need to animate the scrolling. How can I achieve this?
Thanks.
You can create a subclass of NSAnimation to do this. I've created one as part of an open source project of mine (with public domain license).
You can find it here: https://github.com/abhibeckert/Dux/blob/master/Dux/DuxScrollViewAnimation.m (note: this project has ARC enabled. If you're not using ARC you will need to update as appropriate).
Example:
[DuxScrollViewAnimation animatedScrollToPoint:NSMakePoint(x,y) inScrollView:self.enclosingScrollView];
In my app, I set the clipView's boundsOrigin using its animator:
[NSAnimationContext beginGrouping];
NSClipView* clipView = [[myView enclosingScrollView] contentView];
NSPoint newOrigin = [clipView bounds].origin;
newOrigin.x = my_new_origin.x;
[[clipView animator] setBoundsOrigin:newOrigin];
[NSAnimationContext endGrouping];
I'm not sure if this is a supported animation type, but have you tried calling through the animator proxy object?
eg. [[self animator] scrollPoint:NSMakePoint(x, y)];