如何在iOS中使用动画在for循环中绘制从数组中获得的点
我可以用下面的代码画点,但我需要用动画一个一个地画每个点。尝试此操作时,所有点仅在for循环结束后才会显示在屏幕上。如何在iOS中使用动画在for循环中绘制从数组中获得的点,ios,objective-c,drawing,core-animation,calayer,Ios,Objective C,Drawing,Core Animation,Calayer,我可以用下面的代码画点,但我需要用动画一个一个地画每个点。尝试此操作时,所有点仅在for循环结束后才会显示在屏幕上。 我做了一些研究,但我不明白为什么这些点没有在for循环中逐一绘制singerData是一个具有x个点值的数组 -(void)drawDotGraphForUser { UIView *viewAnimationUser = [[UIView alloc]init]; viewAnimationUser.frame = CGRectMake(0, 0, 32
我做了一些研究,但我不明白为什么这些点没有在for循环中逐一绘制
singerData
是一个具有x个点值的数组
-(void)drawDotGraphForUser
{
UIView *viewAnimationUser = [[UIView alloc]init];
viewAnimationUser.frame = CGRectMake(0, 0, 320, 100);
for (int i = 0; i<singerPitchDataSize; i++)
{
CALayer *layer = [CALayer layer];
NSAssert1(layer != nil, @"failed to create CALayer #%i", i);
layer.backgroundColor = [UIColor colorWithRed:0 green:0 blue:1 alpha:1.0].CGColor;
layer.frame = CGRectMake(i+1, singerData[i], 1.0, 1.0);
NSLog(@"GraphUser= %d=%f",i,singerData[i]);
NSLog(@"MSprites=%@",mSprites[i]);
CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
pathAnimation.duration = 5;
pathAnimation.fromValue = [NSNumber numberWithInt:0];
pathAnimation.toValue = [NSNumber numberWithInt:singerPitchDataSize];
[layer addAnimation:pathAnimation forKey:@"strokeEnd"];
[viewAnimationUser.layer addSublayer:layer];
[self.view addSubview:viewAnimationUser];
}
}
-(无效)drawDotGraphForUser
{
UIView*viewAnimationUser=[[UIView alloc]init];
viewAnimationUser.frame=CGRectMake(0,0,320,100);
对于(int i=0;i这些看起来都是同时发生的,因为您的动画正在以极快的速度自动发送到另一个线程。如果您希望它们一个接一个地出现,则需要通过在函数末尾添加计时器将它们以较慢的速度发送到子视图,或者您需要侦听动画离子结束以触发下一个。您是从主线程运行它吗?@Mikael是的,我是这样做的。我只是简单地将此方法称为[self drawDotGraphForUser];谢谢,这很有意义。我将尝试此方法。