In viewDidAppear
I call the following code:
MyView *myView = [[MyView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
myView.backgroundColor = [UIColor clearColor];
[self.view addSubview:myView];
[myView setProgress:0.3 animated:YES];
That last line calls this method:
- (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?
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];
});
What am I doing wrong?
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:
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.