为什么调用我的CABasicAnimation在viewDidAppear中不工作? [英] Why does calling my CABasicAnimation not work in viewDidAppear?

查看:485
本文介绍了为什么调用我的CABasicAnimation在viewDidAppear中不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

viewDidAppear 我调用以下代码:

MyView *myView = [[MyView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
myView.backgroundColor = [UIColor clearColor];

[self.view addSubview:myView];

[myView setProgress:0.3 animated:YES];

最后一行调用此方法:

- (void)setProgress:(CGFloat)progress animated:(BOOL)animated {
    self.innerPie.hidden = NO;

    self.innerPie.strokeEnd = progress;

    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    pathAnimation.duration = 3.0;
    pathAnimation.fromValue = [NSNumber numberWithFloat:self.progress];
    pathAnimation.toValue = [NSNumber numberWithFloat:progress];
    [self.innerPie addAnimation:pathAnimation forKey:@"strokeEndAnimation"];

    self.progress = progress;
}

然而,当我加载模拟器。为什么?

Yet the animation never starts when I load the simulator. Why? Has the view not already appeared, as indicated?

奇怪的是,如果我使用 dispatch_after 延迟0秒

Oddly enough, if I use dispatch_after with a 0 second delay (instant) it will show.

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [myView setProgress:0.8 animated:YES];
});

我做错了什么?

推荐答案

您的解决方案(延迟性能)非常好,所以真的没有问题。唯一的问题是,为什么你的代码不工作没有延迟的性能。我怀疑它与这些行:

Your solution (delayed performance) is excellent, so there's really no problem. The only question is why your code doesn't work without the delayed performance. I suspect it has to do with these lines:

[self.view addSubview:myView];
[myView setProgress:0.3 animated:YES];

您已命令将MyView添加到界面,但显然它没有真的已添加,至少不是以这样的方式,界面已经稳定下来,我们准备添加动画到图层渲染树。您的延迟性能完全满足需要:我们等待一个循环的运行循环,现在的界面(包括添加的MyView)准备摇滚。

You have ordered the MyView to be added to the interface, but apparently it hasn't really been added yet, at least not in such a way that the interface has settled down and we are ready to add an animation to the layer render tree. Your delayed performance accomplishes exactly what's needed: we wait one cycle of the run loop, and now the interface (including the added MyView) is ready to rock and roll.

这篇关于为什么调用我的CABasicAnimation在viewDidAppear中不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆