ios – 使用延迟动画暂停CALayer动画

前端之家收集整理的这篇文章主要介绍了ios – 使用延迟动画暂停CALayer动画前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一组嵌套的UIView动画(在给定时间深2或3级),我希望能够暂停和恢复.其中一些动画使用-animateWithDuration:animations:completion:而其他动画使用-animateWithDuration:delay:options:animations:completion:以延迟动画块的执行.

我阅读并实现了Technical Q&A QA1673关于暂停层树中的所有动画,但我遇到了使用延迟参数的动画的问题.我可以暂停和恢复动画,但是当动画恢复时,任何与其相关的延迟的动画块似乎都会延迟图层树暂停的时间.因此,例如,如果其中一个块的延迟为1秒,并且图层树暂停3秒,则动画在恢复后会延迟4秒.我猜这与beginTime属性有关?任何帮助,将不胜感激.

  1. // Pause and Resume methods,right from the technical Q&A
  2. - (void)pauseAnimationsOnLayer:(CALayer *)layer
  3. {
  4. CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
  5. layer.speed = 0.0;
  6. layer.timeOffset = pausedTime;
  7. }
  8.  
  9. - (void)resumeAnimationsOnLayer:(CALayer *)layer
  10. {
  11. CFTimeInterval pausedTime = [layer timeOffset];
  12. layer.speed = 1.0;
  13. layer.timeOffset = 0.0;
  14. layer.beginTime = 0;
  15. CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
  16. layer.beginTime = timeSincePause;
  17. }
  18.  
  19. // Chained animations
  20. - (void)animateNextPopup
  21. {
  22. [UIView animateWithDuration:kRFPVictorySequenceStatePopupDuration
  23. animations:^{
  24. [_currentStateImageView setHidden:NO];
  25. [_currentStateImageView setTransform:CGAffineTransformIdentity];
  26.  
  27. }
  28. completion:^(BOOL finished) {
  29. [UIView animateWithDuration:kRFPVictorySequenceStateSlideOffDuration
  30. delay:kRFPVictorySequenceStateVoteDelay
  31. options:UIViewAnimationOptionCurveEaseInOut
  32. animations:^{
  33. if (winnerIsDem) {
  34. [_currentStateImageView setFrame:CGRectMake(-_currentStateImageView.frame.size.width,_currentStateImageView.frame.origin.y,_currentStateImageView.frame.size.width,_currentStateImageView.frame.size.height)];
  35. }
  36. else {
  37. [_currentStateImageView setFrame:CGRectMake(1024,_currentStateImageView.frame.size.height)];
  38. }
  39. }
  40. completion:^(BOOL finished) {
  41. // Do some stuff
  42. }
  43. ];
  44. }
  45. ];
  46. }

解决方法

我建议采用不同的方法.

动画块很容易实现,但只有在您不需要对动画进行任何控制时才有用.

否则,您应该使用计时器并手动创建自己的动画.

  1. [NSTimer scheduledTimerWithTimeInterval:0.1
  2. target:self
  3. selector:@selector(timerFired)
  4. userInfo:nil
  5. repeats:YES];
  6.  
  7. - (void)timerFired
  8. {
  9. if (isPaused) {
  10. // Do nothing
  11. } else {
  12. // Animate
  13. }
  14. }
  15.  
  16. - (IBAction)pauseTapped:(id)sender
  17. {
  18. if (isPaused) {
  19. isPaused = NO;
  20. } else {
  21. isPaused = YES;
  22. }
  23. }

isPaused是一个控制动画状态的标志.

猜你在找的iOS相关文章