我可以用下面的代码绘制点,但我需要用动画逐一绘制每个点。当我尝试这一点时,只有在for循环结束后,所有点才出现在屏幕上。
我做了一些研究,但我无法弄清楚为什么这些点不是在for循环中逐一绘制。 singerData
是一个有x个点的数组。如何在iOS中使用动画从for循环中获取数据点
-(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];
}
}
你是从主线程运行它吗? – Mika
@Mikael是的,我做的。我只是简单地调用这个方法[self drawDotGraphForUser]; –