animation结束CALayercallback?

我想知道CALayer中的animation在哪里(或者是否有任何animation)。 具体来说,对于隐含的animation,如改变框架,位置等。在UIView,你可以做这样的事情:

[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]; 

具体来说, setAnimationDidStopSelector是我想要在CALayer中的animation。 有没有这样的事情?

TIA。

您可以使用CATransaction,它有一个完成块处理程序。

 [CATransaction begin]; CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; [pathAnimation setDuration:1]; [pathAnimation setFromValue:[NSNumber numberWithFloat:0.0f]]; [pathAnimation setToValue:[NSNumber numberWithFloat:1.0f]]; [CATransaction setCompletionBlock:^{_lastPoint = _currentPoint; _currentPoint = CGPointMake(_lastPoint.x + _wormStepHorizontalValue, _wormStepVerticalValue);}]; [_pathLayer addAnimation:pathAnimation forKey:@"strokeEnd"]; [CATransaction commit]; 

我回答了我自己的问题。 你必须像这样使用CABasicAnimation来添加一个animation:

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

并执行委托方法animationDidStop:finished:你应该很好走。 谢天谢地,这个function存在! :d

用这个垃圾浪费了4个小时,只是淡出淡出。 请注意代码中的注释。

  [CATransaction begin]; CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"]; animation.duration = 0.3; animation.fromValue = [NSNumber numberWithFloat:0.0f]; animation.toValue = [NSNumber numberWithFloat:1.0f]; animation.removedOnCompletion = NO; animation.fillMode = kCAFillModeBoth; /// [box addAnimation:animation forKey:@"j"]; Animation will not work if added here. Need to add this only after the completion block. [CATransaction setCompletionBlock:^{ CABasicAnimation *animation2 = [CABasicAnimation animationWithKeyPath:@"opacity"]; animation2.duration = 0.3; animation2.beginTime = CACurrentMediaTime()+1; animation2.fromValue = [NSNumber numberWithFloat:1.0f]; animation2.toValue = [NSNumber numberWithFloat:0.0f]; animation2.removedOnCompletion = NO; animation2.fillMode = kCAFillModeBoth; [box addAnimation:animation2 forKey:@"k"]; }]; [box addAnimation:animation forKey:@"j"]; [CATransaction commit]; 

这是基于bennythemink解决scheme的Swift 3.0中的一个答案:

  // Begin the transaction CATransaction.begin() let animation = CABasicAnimation(keyPath: "strokeEnd") animation.duration = duration //duration is the number of seconds animation.fromValue = 0 animation.toValue = 1 animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear) circleLayer.strokeEnd = 1.0 // Callback function CATransaction.setCompletionBlock { print("end animation") } // Do the actual animation and commit the transaction circleLayer.add(animation, forKey: "animateCircle") CATransaction.commit() 

对于在Google上查找此页面的用户,请注意:通过将animation对象的“委托”属性设置为将接收通知并在该对象的.m中实现“animationDidStop”方法的对象,您完全可以完成工作文件。 我只是试了一下,而且工作。 我不知道为什么乔·布洛说这不是正确的方法。

您可以在设置CAAnimation对象时设置给定animation的名称。 在animationDiStop中:完成后,只需比较提供的animation对象的名称,以根据animation执行特定的function。