Animation End Callback for CALayer?

2019-01-08 09:07发布

I'm wondering where the callbacks are (or if there are anything) for animations in a CALayer. Specifically, for implied animations like altering the frame, position, etc. In a UIView, you could do something like this:

[UIView beginAnimations:@"SlideOut" context:nil];
[UIView setAnimationDuration:.3];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animateOut:finished:context:)];
CGRect frame = self.frame;
frame.origin.y = 480;
self.frame = frame;
[UIView commitAnimations];

Specifically, the setAnimationDidStopSelector is what I want for an animation in a CALayer. Is there anything like that?

TIA.

9条回答
地球回转人心会变
2楼-- · 2019-01-08 09:28

I answered my own question. You have to add an animation using CABasicAnimation like so:

CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath:@"frame"];
anim.fromValue = [NSValue valueWithCGRect:layer.frame];
anim.toValue = [NSValue valueWithCGRect:frame];
anim.delegate = self;
[layer addAnimation:anim forKey:@"frame"];

And implement the delegate method animationDidStop:finished: and you should be good to go. Thank goodness this functionality exists! :D

查看更多
别忘想泡老子
3楼-- · 2019-01-08 09:30

Just a note for those who find this page on Google: You really can get the job done by setting the "delegate" property of your animation object to the object that will receive the notification and implementing the "animationDidStop" method in that object's .m file. I just tried it, and it works. I don't know why Joe Blow said that's not the correct way.

查看更多
放荡不羁爱自由
4楼-- · 2019-01-08 09:33

For 2018 ...

Swift 4.

use .setCompletionBlock

In practice you need [weak self] or you'll typically crash.

func animeExample() {

    CATransaction.begin()

    let a = CABasicAnimation(keyPath: "fillColor")
    a.fromValue, duration = ... etc etc

    CATransaction.setCompletionBlock{ [weak self] in
        self?.animeExample()
    }

    someLayer.add(a, forKey: nil)
    CATransaction.commit()
}

In the example, it just calls itself again.

Of course, you can call any function.


Note: if you're just getting started. It's worth remembering that

  1. the "key" (as in add#forKey) is irrelevant and rarely used. Set it to nil. If for some reason you want to set it, set it to "any string" (say, your nickname). On the other hand...

  2. The keyPath in the CABasicAnimation call is in fact the actual "thing you are animating", in other words it's literally a property of the layer (but just written as a string).

In short add#forKey is almost always just nil, it is irrelevant.

You often see code where these two are confused (thanks to the silly naming by apple), which causes all sorts of problems.


Do note that as of recently you can use animationDidStop with the delegate, see answer by @jack below! In some cases that is easier, sometimes it's easier to just use a completion block. If you have many different animes (which is often the case), just use completion blocks.

查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-01-08 09:38

If you don't want to mess around with CATransaction. I've created an extension that encapsulates the callback in the animation object.

The code can be found in this snippet: https://gitlab.com/snippets/1786298

And you can then use it like this:

let appearAnimation = CASpringAnimation(keyPath: "transform")
appearAnimation.fromValue = contentView.layer.transform
appearAnimation.toValue = CATransform3DIdentity
appearAnimation.mass = 0.65
appearAnimation.duration = appearAnimation.settlingDuration
appearAnimation.isAdditive = true
appearAnimation.onCompletion {
    completed()
}
查看更多
啃猪蹄的小仙女
6楼-- · 2019-01-08 09:40

In Swift 4+ i have just added delegate as

class CircleView: UIView,CAAnimationDelegate {
...

let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.delegate = self//Set delegate

Animation completion callback -

func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
     print("Animation END")
  }
查看更多
甜甜的少女心
7楼-- · 2019-01-08 09:40

You can set the name of a given animation when setting up the CAAnimation object. In animationDiStop:finished, just compare the name of theAnimation object provided to perform you specific functionality based on the animation.

查看更多
登录 后发表回答