I am using CABasicAnimation to move and resize an image view. I want the image view to be added to the superview, animate, and then be removed from the superview.
In order to achieve that I am listening for delegate call of my CAAnimationGroup, and as soon as it gets called I remove the image view from the superview.
The problem is that sometimes the image blinks in the initial location before being removed from the superview. What's the best way to avoid this behavior?
CAAnimationGroup *animGroup = [CAAnimationGroup animation];
animGroup.animations = [NSArray arrayWithObjects:moveAnim, scaleAnim, opacityAnim, nil];
animGroup.duration = .5;
animGroup.delegate = self;
[imageView.layer addAnimation:animGroup forKey:nil];
When you add an animation to a layer, the animation does not change the layer's properties. Instead, the system creates a copy of the layer. The original layer is called the model layer, and the duplicate is called the presentation layer. The presentation layer's properties change as the animation progresses, but the model layer's properties stay unchanged.
When you remove the animation, the system destroys the presentation layer, leaving only the model layer, and the model layer's properties then control how the layer is drawn. So if the model layer's properties don't match the final animated values of the presentation layer's properties, the layer will instantly reset to its appearance before the animation.
To fix this, you need to set the model layer's properties to the final values of the animation, and then add the animation to the layer. You want to do it in this order because changing a layer property can add an implicit animation for the property, which would conflict with the animation you want to explicitly add. You want to make sure your explicit animation overrides the implicit animation.
So how do you do all this? The basic recipe looks like this:
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:#"position"];
animation.fromValue = [NSValue valueWithCGPoint:myLayer.position];
layer.position = newPosition; // HERE I UPDATE THE MODEL LAYER'S PROPERTY
animation.toValue = [NSValue valueWithCGPoint:myLayer.position];
animation.duration = .5;
[myLayer addAnimation:animation forKey:animation.keyPath];
I haven't used an animation group so I don't know exactly what you might need to change. I just add each animation separately to the layer.
I also find it easier to use the +[CATransaction setCompletionBlock:] method to set a completion handler for one or several animations, instead of trying to use an animation's delegate. You set the transaction's completion block, then add the animations:
[CATransaction begin]; {
[CATransaction setCompletionBlock:^{
[self.imageView removeFromSuperview];
}];
[self addPositionAnimation];
[self addScaleAnimation];
[self addOpacityAnimation];
} [CATransaction commit];
CAAnimations are removed automatically when complete. There is a property removedOnCompletion that controls this. You should set that to NO.
Additionally, there is something known as the fillMode which controls the animation's behavior before and after its duration. This is a property declared on CAMediaTiming (which CAAnimation conforms to). You should set this to kCAFillModeForwards.
With both of these changes the animation should persist after it's complete. However, I don't know if you need to change these on the group, or on the individual animations within the group, or both.
Heres an example in Swift that may help someone
It's an animation on a gradient layer. It's animating the .locations property.
The critical point as #robMayoff answer explains fully is that:
Surprisingly, when you do a layer animation, you actually set the final value, first, before you start the animation!
The following is a good example because the animation repeats endlessly.
When the animation repeats endlessly, you will see occasionally a "flash" between animations, if you make the classic mistake of "forgetting to set the value before you animate it!"
var previousLocations: [NSNumber] = []
...
func flexTheColors() { // "flex" the color bands randomly
let oldValues = previousTargetLocations
let newValues = randomLocations()
previousTargetLocations = newValues
// IN FACT, ACTUALLY "SET THE VALUES, BEFORE ANIMATING!"
theLayer.locations = newValues
// AND NOW ANIMATE:
CATransaction.begin()
// and by the way, this is how you endlessly animate:
CATransaction.setCompletionBlock{ [weak self] in
if self == nil { return }
self?.animeFlexColorsEndless()
}
let a = CABasicAnimation(keyPath: "locations")
a.isCumulative = false
a.autoreverses = false
a.isRemovedOnCompletion = true
a.repeatCount = 0
a.fromValue = oldValues
a.toValue = newValues
a.duration = (2.0...4.0).random()
theLayer.add(a, forKey: nil)
CATransaction.commit()
}
The following may help clarify something for new programmers. Note that in my code I do this:
// IN FACT, ACTUALLY "SET THE VALUES, BEFORE ANIMATING!"
theLayer.locations = newValues
// AND NOW ANIMATE:
CATransaction.begin()
...set up the animation...
CATransaction.commit()
however in the code example in the other answer, it's like this:
CATransaction.begin()
...set up the animation...
// IN FACT, ACTUALLY "SET THE VALUES, BEFORE ANIMATING!"
theLayer.locations = newValues
CATransaction.commit()
Regarding the position of the line of code where you "set the values, before animating!" ..
It's actually perfectly OK to have that line actually "inside" the begin-commit lines of code. So long as you do it before the .commit().
I only mention this as it may confuse new animators.
Related
I'm trying to create multiple "cards" to animate them afterwards using this code:
CAReplicatorLayer *cardsWrapperLayer = [CAReplicatorLayer layer];
cardsWrapperLayer.instanceCount = 4;
cardsWrapperLayer.instanceDelay = 10;
cardsWrapperLayer.instanceTransform = CATransform3DMakeTranslation(0, phoneSize.height + self.phonePadding, 0);
[cardsWrapperLayer addSublayer:self.cardLayer];
but they are appearing all at the same time, even if the instanceDelay is set to 10. I have this piece of code in the viewDidAppear method.
instanceDelay doesn’t do anything by itself, it just shifts the “current time” for each instance. To see something happen, you need to add an animation, like this:
CABasicAnimation *fadeIn = [CABasicAnimation animationWithKeyPath:#"opacity"];
fadeIn.fromValue = #0; // if we don’t specify a toValue, it’ll animate to the layer’s current value which by default is 1
fadeIn.duration = 0.2;
fadeIn.removedOnCompletion = NO;
[self.cardLayer addAnimation:appear forKey:#"appear"];
Note that the removedOnCompletion is important—if you let the animation remove itself automatically, then it’ll be gone as soon as the first instance finishes animating and the other instances will snap to their final state. You should remove the animation manually at a later time, like when you know it’ll be over (i.e. the animation’s duration ✕ the replicator layer’s instanceCount)—just call -removeAnimationForKey: on the base layer with the key you added the animation with.
You also have to make sure that you set the animation on the replicator layer's sublayer, not on the replicator layer itself! Bit me hard after 3 years of not working with CAReplicatorLayer again.
I have a UIView. I applied the animation to its CALayer.
[view.layer addAnimation:groupAnimation forKey:name];
I want the final state of the layer to be the state of the UIView after the animation. Let's say I rotated by 45degrees and moved to a new position using the layer; is it possible for my view to be in that state after the animation? Because right now, after the animation, it goes back to the original state of the UIView. I hope to receive some help with this. Thanks.
The thing to understand is that layer animation is just animation; it's merely a kind of temporary "movie" covering the screen. When the animation ends, the "movie" is removed, thus revealing the true situation. It is up to you to match that situation with the final frame of the movie.
UIView animation does this for you, to a great extent. But layer animation leaves it entirely up to you.
Thus, let's say you animate a position change; doing something so that the layer or view actually is where you animated to is completely up to you.
The usual thing is to perform the changes yourself as a separate set of commands. But be careful not to do anything that might trigger implicit layer animation, as this will conflict with the animation you are trying to perform explicitly.
Here's example code (not related to yours, but it does show the general form):
CAAnimationGroup* group = // ...
// ... configure the animation ...
[view.layer addAnimation:group forKey:nil];
// now we are ready to set up the view to look like the final "frame" of the animation
[CATransaction setDisableActions:YES]; // do not trigger implicit animation by mistake
view.layer.position = finalPosition; // assume we have worked this out
When animating a CALayer or using a CAAnimationGroup, the following properties must be set, e.g.:
groupAnimation.removedOnCompletion = NO;
groupAnimation.fillMode = kCAFillModeForwards;
See also: After Animation, View position resets
Also note that it may be helpful to apply animations directly to the view itself, rather than by accessing the view's layer. This is accomplished using animation blocks, which I have found to be very useful.
Block style animations can be customized in many ways, but here's a basic example, which could be invoked within a function when your view needs to animate:
- (void) animateMyView
{
CGRect newViewFrame = CGRectMake(x,y,w,h);
[UIView animateWithDuration:0.5
delay:0
options: (UIViewAnimationOptionCurveLinear )
animations:^{
self.myView = CGAffineTransformMakeRotation (45);
[self.myView setBounds:newViewFrame];
}
completion:NULL];
}
For more information, see Apple's documentation on View Animation.
My app uses CALayer to draw views. More precisely, it uses the drawLayer:inContext: method on a sublayer of a UIView's top layer. This is a nice way to get the 'implicit' animation of consecutive drawLayer:inContext: drawings to fade into each other over time. The fading animations happen fairly fast, maybe in 0.25 seconds, but to change its duration, simply implement another delegate method called actionForLayer:forKey:. In this perfectly working example implementation here the default duration is stretched to 2.0 seconds:
- (id<CAAction>)actionForLayer:(CALayer *)layer forKey:(NSString *)event
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:event];
animation.duration = 2.0;
return animation;
// or return nil for the default duration.
}
On to the issue at hand.
If you call [sublayer setNeedsDisplay] faster than the fades have time to complete, with each new fade you'll see a sudden jump. From the looks of it, the fade that's in progress is cancelled and it's final state is used as the starting point of the new fade. This might not be very surprising, but the visual result is rather unwanted.
Consider the scenario of a ten second fade from black to white, with another fade, to black, triggered five seconds after the start. The animation will start fading from black to white, but when it's at a 'half way gray' it jumps to full white before fading to black again.
Is there a way to prevent this from happening? Can I get the layer to fade from the gray back down to black? Is there a CALayer drawing equivalent of saying UIViewAnimationOptionBeginFromCurrentState (used in UIView animations)?
Cheers.
A layer's animation is only a visual representation of what the layer should look like as it animates. In CA when you animate from one state to another, the entire state of the layer changes immediately. A presentation layer is created and displays the animation, and when the animation completes the actual layer is left in place at the end.
So, my guess is that when you want to transition from one state to another, and the current animation hasn't completed yet, you have to capture the current state of the animation and then use this as the starting point for your next animation.
The problem lies in not being able to modify a layer's current animation.
In the following post I capture the current state of an animation, set that as the current state for the layer and use that as the beginning value from which to animate. The post applies this technique to the speed / duration of an animation, but can also be applied to your scenario.
https://stackoverflow.com/a/9544674/1218605
I'm a little stumped on this one too.
Did you forget to specify the fillMode kCAFillModeForwards. There's more info about that in the reference docs.
For example, I got this to work without any snapping, although I'm not changing the duration.
#implementation FadingLayer
- (void)fadeOut {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:#"backgroundColor"];
animation.fillMode = kCAFillModeForwards;
animation.fromValue = (id)[UIColor redColor].CGColor;
animation.toValue = (id)[UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.0].CGColor;
animation.removedOnCompletion = FALSE;
animation.delegate = self;
[self addAnimation:animation
forKey:#"test"];
}
- (void)fadeIn {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:#"backgroundColor"];
animation.fillMode = kCAFillModeForwards;
animation.fromValue = (id)[UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.0].CGColor;
animation.toValue = (id)[UIColor redColor].CGColor;
animation.removedOnCompletion = FALSE;
animation.delegate = self;
[self addAnimation:animation
forKey:#"test"];
}
#end
You'll probably want to animate a custom property however.
Hope this helps :/
I wanted to accomplish the same thing with a zoom animation of a layer tree. I have a zoom in/out key-equivalent where the user can zoom the layer tree accordingly. However, if the user presses the zoom key-equivalent in rapid succession, there would be a temporary snap-back to the values prior to the onset of the animation, since the previous animation hadn't yet completed.
At the end of the animation code, performing a sole [CATransaction commit] forced any pending transactions to be committed to the layer model before the start of the next animation, and solved the problem.
The documentation says:
+ commit
Commit all changes made during the current transaction.
Declaration
+ (void)commit
Special Considerations
Raises an exception if no current transaction exists.
However, testing this with many [CATransaction commit] messages in succession doesn't actually raise an exception. I've used this same technique to squelch warnings of the form:
CoreAnimation: warning, deleted thread with uncommitted
CATransaction;
in an NSOperation whose thread of execution finishes before layer animations do. It could be that Apple changed this behaviour in recent OS releases to a no-op (which would be much saner) if no current transaction exists, without updating the documentation.
I have multiple animate-able/interactive UIImageViews on my UIView which would animate/interact based on user touches. I am trying to add a feature which would "wiggle" these animate-able/interactive UIImageViews on touch of an Icon which would help user in identifying which objects are interactive on screen. I am trying to use following code to achieve "wiggle" movement from this blog:
-(void)doWiggle:(UIView *)touchView {
// grabbing the layer of the tocuhed view.
CALayer *touchedLayer = [touchView layer];
// here is an example wiggle
CABasicAnimation *wiggle = [CABasicAnimation animationWithKeyPath:#"transform"];
wiggle.duration = 0.1;
wiggle.repeatCount = 1e100f;
wiggle.autoreverses = YES;
wiggle.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(touchedLayer.transform,0.1, 0.0 ,1.0 ,1.0)];
// doing the wiggle
[touchedLayer addAnimation:wiggle forKey:#"wiggle"];
// setting a timer to remove the layer
[NSTimer scheduledTimerWithTimeInterval: 2.0 target:self selector:#selector(endWiggle:) userInfo:touchedLayer repeats:NO];
}
-(void)endWiggle:(NSTimer*)wiggleTimer {
// stopping the wiggle now
[((CALayer*)wiggleTimer.userInfo) removeAllAnimations];
}
I call it like this in my routine :[self doWiggle:imageAnimation]; where imageAnimation is an UIImageView. I see that while wiggling, half of the image is disappearing. I read somewhere that I need to set the z-index properly in order for this to work. What do I need to set for z-index? and I have 3 to 4 UIImageViews per page where I need to call doWiggle: routine, do I need to fix z-indexes of all those UIImageViews?
Before you call doWiggle, you may need to do this code:
[[touchView superview] bringSubviewToFront:touchView];
If you can see the entire view before doing the animation then this will probably not fix your problem. It is possible that your image is being scaled by the image view and that is causing a problem during the animation. You may need to rescale your image, with an image editing program, to fit inside the view without having the view scale it. You can also do this programmatically, if you don't have a static image (i.e. loading off the network, etc).
Core Animation allows for custom animations by implementing the actionForKey method in your CALayer based class:
- (id<CAAction>)actionForKey:(NSString *)key {
// Custom animations
return [super actionForKey:key];
}
I can then create an animation and return it for the onOrderIn action (i.e. when the layer is added to another layer). This works just fine. If I do the same for onOrderOut (i.e. the layer is removed from its superlayer), the returned animation is ignored, and the default animation is applied instead.
My goal is to zoom the layer in (onOrderIn) and out (onOrderOut):
- (id<CAAction>)actionForKey:(NSString *)key {
if ([key isEqualToString:#"onOrderIn"] || [key isEqualToString:#"onOrderOut"]) {
CABasicAnimation *a = [CABasicAnimation animationWithKeyPath:#"transform.scale"];
a.duration = 0.25;
a.removedOnCompletion = NO;
a.fillMode = kCAFillModeBoth;
if ([key isEqualToString:#"onOrderIn"]) {
a.fromValue = [NSNumber numberWithFloat:0.0];
a.toValue = [NSNumber numberWithFloat:1.0];
} else {
a.fromValue = [NSNumber numberWithFloat:1.0];
a.toValue = [NSNumber numberWithFloat:0.0];
}
return a;
}
return [super actionForKey:key];
}
Zooming in works, zooming out does not. Instead the default fade out animation is used.
The code might contain some typos, as I'm typing this on another machine.
Can anyone help?
Quoting John Harper on quartz-dev mailing list:
There's a fundamental problem with
returning any animation for the
onOrderOut key—by the time the
animation should be running, the layer
is no longer in the tree, so it has no
effect. So onOrderOut is not useful
for triggering animations; it may be
useful for running other code when
layers are removed from the tree.
The best solution I've found for this
(assuming the default fade transition
on the parent is not what you want,
which it often isn't) is to add custom
animations to apply the removal effect
you want, then, in the didStop
animation delegate, actually remove
the layer. It's often convenient to
create a single group of animations
with the delegate property set, and
fillMode=forwards,
removedOnCompletion=NO so that you can
remove the layer at the end of the
animation with no possibility of the
layer still being visible in its
normal state.
If you do many case of this, it is easy to write a common superclass that starts an animation, sets the animation delegate to the class and implements +animationDidStop: to remove the layer w/o animation enabled. This restores the fire-and-forget nature of CoreAnimation that you'd have hoped would be present with the default implementation.
Have you verified that your method is being called with key as #"onOrderOut" and that your method is returning the correct animation?