【问题标题】:performSelector:withObject:afterDelay called after the end of a CALayer animationperformSelector:withObject:afterDelay 在 CALayer 动画结束后调用
【发布时间】:2013-01-14 22:13:57
【问题描述】:

我想对一些 CALayer 的位置进行动画处理。在动画结束之前,我想推另一个UIViewController,这样当我弹出最后一个 UIView 控制器时,CALayers 会回到原来的位置。这是我的代码:

CABasicAnimation *animation4 = [CABasicAnimation animationWithKeyPath:@"position"];
animation4.fromValue = [control.layer valueForKey:@"position"];

CGPoint endPoint4=CGPointMake(512, -305);

animation4.toValue =[NSValue valueWithCGPoint:endPoint4];
animation4.duration=1;
[control.layer addAnimation:animation4 forKey:@"position"];

[self performSelector:@selector(goToSolutionViewController) withObject:nil afterDelay:0.9];

goToSolutionViewController 我有:

-(void)goToSolutionViewController{

    SolutionViewController *solution=[self.storyboard instantiateViewControllerWithIdentifier:@"SolutionViewID"];

    [self.navigationController pushViewController:solution animated:NO];

}

问题是

[self performSelector:@selector(goToSolutionViewController) withObject:nil afterDelay:0.9]

直到动画结束才被调用。所以goToSolutionViewController 在 1.9 秒后被调用,而不是 0.9 秒。

如何在动画结束前推送 UIViewController?或者当我弹出UIViewController 时让CALayers 回到原来的位置,但用户看不到返回的路。

编辑:---

此性能问题仅在我第一次执行动画并推送 UIViewcontroller 时发生。当我弹出它并再次执行所有操作时,性能就像幽灵一样。问题可能出在 UIViewController 第一次加载时间。

【问题讨论】:

  • 将 performSelector 行移到动画块之前(?)
  • 我有相同的结果,但我注意到它只在我第一次这样做时发生。如果我弹出 solutionViewController 并再次执行所有操作,它会完美运行。可能是navigationcontroller第一次push uiviewcontroller的时间比较长。如何控制它?

标签: ios objective-c core-animation calayer performselector


【解决方案1】:

与延迟后执行相比,您应该使用动画回调之一来调用您的方法,而不是依赖动画的时间。您可以使用 CATransaction,它允许您使用块,也可以使用普通的委托方法。

使用 CATransaction

通过将您的动画(将其添加到图层)包装在事务中,您可以使用事务的完成块。

[CATransaction begin];
// Your animation here...
[CATransaction setCompletionBlock:^{
    // Completion here...
}];
[CATransaction commit];

使用委托回调

通过将自己设置为动画委托,您会在动画结束时获得委托回调。

animation4.delegate = self;

还有回调

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    // Completion here..
}

【讨论】:

  • 问题是我想在动画结束之前调用方法。
  • @user1419017 这没什么意义。为什么不制作更短的动画?
  • 当我在另一个 UIViewController 中时,我只是想让我的 CALayers 回到原来的位置,所以我看不到返回的路,当我弹出最后一个 UIViewController 时,我有相同的 CALayers地点。
  • @user1419017 除非你真的更新了层的属性,否则一旦动画结束它就会回到原来的位置。
猜你喜欢
  • 2010-09-22
  • 1970-01-01
  • 2011-09-05
  • 2013-12-15
  • 1970-01-01
  • 2021-07-23
  • 2021-10-18
  • 2018-08-01
相关资源
最近更新 更多