Why doesn't calling CABasicAnimation work in viewDidAppear?

In viewDidAppearI 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];

This 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;
}

But the animation never starts when I load the simulator. What for? The view did not appear as indicated?

Oddly enough, if I use dispatch_afterwith 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?

+4
source share
1 answer

Your solution (deferred performance) is excellent, so no problem. The only question is why your code does not work without delayed performance. I suspect this is due to these lines:

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

, MyView , , -, , , , , . , : , ( MyView) .

+4

Source: https://habr.com/ru/post/1532075/


All Articles