0

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.

cessmestreet
  • 2,298
  • 3
  • 22
  • 42

2 Answers2

0

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
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • And see my full tutorial on this topic starting about here: http://www.apeth.com/iOSBook/ch17.html#_using_a_cabasicanimation – matt Jul 27 '14 at 16:41
-1

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.

Community
  • 1
  • 1
JRJ
  • 65
  • 3
  • my auto layout is disabled but still after the animation, it goes back to its original state. Actually, I tried doing UIViewAnimation but I couldn't achieve what I want. CALayer animations are more flexible. Is there a way I can make sure that my UIView will be in the same state as its layer which I animated? – cessmestreet Jul 25 '14 at 07:25
  • Answer updated to include the following information: When animating a CALayer or using a CAAnimationGroup, the following properties must be set: groupAnimation.removedOnCompletion = NO; groupAnimation.fillMode = kCAFillModeForwards; See also: http://stackoverflow.com/questions/226555/after-animation-view-position-resets – JRJ Jul 27 '14 at 16:18
  • misusing `removedOnCompletion` in this way is absolutely the wrong thing to do, and should not be given as advice – matt Jul 27 '14 at 16:37
  • @matt, when is using `removedOnCompletion` and `fillMode` the right thing to do? Couldn't one work with layers in the presentation tree, rather than maintain parity between the last animation frame and the actual layer? – JRJ Jul 28 '14 at 22:21
  • `fillMode` is about the relationship between different animations that start/end at different times within a group. `removedOnCompletion` is almost never needed. – matt Jul 28 '14 at 23:04