OS X 10.7 Animation Did stop selector - objective-c

I am trying to figure out how to set a call back for when my nsview animator stops. Anyone know how to do this.
NSRect frame = blob.frame;
frame.origin.x = animationStopX;
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:timeToDisappear];
[[blob animator] setFrame:frame];
[NSAnimationContext endGrouping];

On 10.8, NSAnimationContext has completionHandler property which you can use with a block.

You can set delegate for frameOrigin animation.
CAAnimation *moveAnimation = [[blob animationForKey:#"frameOrigin"] copy];
moveAnimation.delegate = self;
[blob setAnimations:[NSDictionary dictionaryWithObject:moveAnimation forKey:#"frameOrigin"]];
[moveAnimation release];
And override end animation delegate method
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag

Related

Disable roll-out animation for NSOutlineView

Is there a way to stop the roll-out animation for an NSOutlineView. By 'roll-out' I mean the animation that happens when an item is expanded/collapsed, and the children slide down/up.
Create a subclass of NSOutlineView and suppress animation:
-(void) expandItem:(id)item expandChildren:(BOOL)expandChildren
{
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:0.0];
[super expandItem:item expandChildren:expandChildren];
[NSAnimationContext endGrouping];
}
Other methods are:
– expandItem:
– expandItem:expandChildren:
– collapseItem:
– collapseItem:collapseChildren:

Mavericks issue: CAKeyframeAnimation on window does not animate

I am working on adapting my app to Mavericks since I have been developing it for 10.8. So far I have found various issues which I can't seem to be able to solve. One of them has to do with NSPopover-like animations.
I have a window which I animate in this way:
_zoomWindow.alphaValue = 0;
[_zoomWindow orderFront: self];
// Configure bouncing animation
CAKeyframeAnimation* frameAnim = [CAKeyframeAnimation animation];
[frameAnim setTimingFunction: [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut]];
[frameAnim setValues: #[[NSValue valueWithRect: startFrame], [NSValue valueWithRect: overshootFrame], [NSValue valueWithRect: endFrame]]];
[frameAnim setDuration: duration];
[frameAnim setDelegate: self];
[_zoomWindow setAnimations: #{#"frame": frameAnim}];
// Configure alpha animation
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration: duration];
[[_zoomWindow animator] setAlphaValue: 1.0];
[[_zoomWindow animator] setFrame: endFrame display: YES];
[NSAnimationContext endGrouping];
This works beautifully on 10.8! But it just doesn't do anything on 10.9. Am I missing something here?
I figured it out. The result was that it didn't show anything while suposedly animating, so at first I thought it wasn't animating at all. As it turns out, the problem was in another part of my code completely: the window that I was animating had the content view subclassed by me, and that view was doing some custom drawing. The real problem was that the view wasn't drawing anything on 10.9, even though it was drawing on 10.8... Once I fixed the drawing issue, the animation worked perfectly.

Rotating NSView around center

I'm trying to animate the rotation of an NSView around its center, but it keeps shifting side to side during rotation. What is causing this?
-(void)startRefreshAnimation {
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:1.0];
[[NSAnimationContext currentContext] setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
[NSAnimationContext currentContext].completionHandler = ^{ [self startRefreshAnimation]; };
[[view animator] setFrameCenterRotation:previousRotation - 90.0];
previousRotation += -90.0;
[NSAnimationContext endGrouping];
}
Shift up during rotation:
Shift down during rotation:
here is the dummy project and the answer that I found. (For Cocoa Applicaiton)
Rotate NSImageView
github project (download)
From the documentation:
If the application has altered the layer’s anchorPoint property, the behavior is undefined. Sending this message to a view that is not managing a Core Animation layer causes an exception.
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSView_Class/Reference/NSView.html
Is your view managing a CALayer with an unmodified anchor point?
EDIT
I setup similar code at got the exact same results. No adjust of origin or anchor point could resolve this problem. My theory is that this particular method contains bugs (does for autolayout), or works in a way which we don't anticipate. I achieved the correct effect using CABasicAnimation.
/* setup */
....
_view.layer.anchorPoint = CGPointMake(0.5f, 0.5f);
_view.layer.position = ...
[self startRefreshAnimation];
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
[self startRefreshAnimation];
}
-(void)startRefreshAnimation {
CABasicAnimation *anim2 = [CABasicAnimation animationWithKeyPath:#"transform.rotation"];
anim2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
anim2.fromValue = [NSNumber numberWithFloat:previousRotation * (M_PI / 180.0f)];
anim2.toValue = [NSNumber numberWithFloat:(previousRotation + 90.0f) * (M_PI / 180.0f)];
previousRotation = previousRotation + 90.0f;
anim2.duration = 1.0f;
anim2.delegate = self;
[_view.layer addAnimation:anim forKey:#"transform"];
}

CABasicAnimation move frame left by 300px

I need help with CABasicAnimation. I am trying to move a NSView left by 300 pixels. I found this SO thread: How to animate the frame of an layer with CABasicAnimation?
Turns out animating the frame is not possible and one of the answer points to a link to QA on Apple's website but it takes me a to a generic page:
http://developer.apple.com/library/mac/#qa/qa1620/_index.html
So, how can I do something as simple as translation of my NSView/CALyer?
Thanks!
NSView has a protocol called NSAnimatablePropertyContainer which allows you to create basic animations for views:
The NSAnimatablePropertyContainer protocol defines a way to add
animation to an existing class with a minimum of API impact ...
Sending of key-value-coding compliant "set" messages to the proxy will
trigger animation for automatically animated properties of its target
object.
The NSAnimatablePropertyContainer protocol can be found here
I recently used this technique to change the origin of a frame:
-(void)setOrigin:(NSPoint)aPoint {
[[self animator] setFrameOrigin:aPoint];
}
Instead of calling the [view setFrameOrigin:], I created another method called setOrigin: which then applies the setFrameOrigin: call to the view's animator.
If you need to change the duration of the animation, you can do so like this (similar to CATransactions):
-(void)setOrigin:(NSPoint)aPoint {
[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setCompletionHandler:^{
...Completion Callback Code goes here...
}];
[[NSAnimationContext currentContext] setDuration:1.0];
[[self animator] setFrameOrigin:aPoint];
[NSAnimationContext endGrouping];
}
The NSAnimationContext is described here
You can animate center property instead. Eg:
//assuming view is your NSView
CGPoint newCenter = CGPointMake(view.center.x - 300, view.center.y);
CABasicAnimation *animation = [CABasicAnimation animation];
//setup your animation eg. duration/other options
animation.fromValue = [NSValue valueWithCGPoint:v.center];
animation.toValue = [NSValue valueWithCGPoint:newCenter];
[view.layer addAnimation:animation forKey:#"key"];

How do I trigger a callback when a NSAnimationContext ends?

I have an animation which moves some views around. When this animation completes I want the window to recalculate the keyview loop. My code is simmilar to the follow mock code:
[NSAnimationContext beginGrouping];
[newView setAlpha: 0.0]; //hide newView
[self addSubView:newView];
//position the views
[[oldView animator] setFrame: newFrame1];
[[newView animator] setFrame: newFrame2];
[[newView animator] setAlpha: 1.0]; //fade-in newView
[NSAnimationContext endGrouping];
[[self window] recalculateKeyViewLoop];
The problem with this code is that recalculateKeyViewLoop is called before the views are in their new positions which means that the keyviewloop is wrong.
How do I fix this?
My first though is to call recalculateKeyViewLoop in a callback from when the animation ends but I can't figure out how to do this.
Something that's not so obvious, or at least wasn't to me, is that there are two animations going on when you do a setFrame:, with keys frameSize and frameOrigin.
Depending on what your original and final frames are you may need to register yourself as a delegate for one or both of them.
I'd also recommend that you make a copy of the animation you get back from -animationForKey: and store your modified copy in the animations dictionary of your object. This way your delegate will only be called at the conclusion of that particular objects' animator duration, versus all objects animating that key.
eg.
CAAnimation *animation = [[view animationForKey:#"frameOrigin"] copy];
animation.delegate = self;
[view setAnimations:[NSDictionary dictionaryWithObject:animation forKey:#"frameOrigin"]];
In this way your animation object will supersede the default animation object for that view. Then implement whichever delegate methods you're interested in.
You should be able to send -animationForKey: to your views to get a CAAnimation instance, then set yourself as its delegate and implement the method that Adam mentioned.
if you use CAAnimation this has an animationDidStop:finished: delegate method..
http://developer.apple.com/documentation/GraphicsImaging/Reference/CAAnimation_class/Introduction/Introduction.html#//apple_ref/occ/cl/CAAnimation
Hope this helps
You can use a completion handler like this:
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context){
// Start some animations.
[[myView animator] setFrameSize:newViewSize];
[[myWindow animator] setFrame:newWindowFrame display:YES];
} completionHandler:^{
// This block will be invoked when all of the animations started above have completed or been cancelled.
NSLog(#"All done!");
}];
Check out my post here: How would I do this iOS animation on OSX?
I wrote a class that handles this for you, using blocks. Hopefully your target allows blocks! :)
If you are animating an NSWindow (as opposed to an NSView), the other answers will not work and you should do this instead:
CAAnimation *animation = [CABasicAnimation animation];
animation.delegate = self;
self.window.animations = #{#"frame": animation};
[[self.window animator] setFrame:NSMakeRect(0, 0, 400, 200) display:YES];
which will then call the animationDidStop:finished: method on your delegate (which in the above code would be self)