4

我有一组uibezierpath。我怎么能按顺序绘制每条路径,但有延迟,所以它看起来是动画的?我目前正在像这样绘制整个数组(从技术上讲,我认为它是连续的,但速度如此之快,它一下子就出现了):

for (UIBezierPath * path in myArrayOfBezierPaths){
    path.lineWidth=3;
    [path stroke];
    [path release];
}

最后,我怎样才能让每条路径都有不同的颜色?

4

1 回答 1

3

您需要多次重绘视图,每次绘制更多路径。为此,您需要使用某种计时器,并有办法跟踪您正在绘制的路径。

要跟踪路径,您可以使用NSInteger实例变量并在每次绘制另一条路径时递增它,然后-drawRect:检查该变量的值并绘制适当数量的路径。

然后,您将实现一个更新 ivar 并重绘视图的方法。这是一个非常简单的版本:

//assume pathsToDraw is an NSInteger ivar
//and that myArrayOfBezierPaths is an NSArray ivar
//containing UIBezierPath objects


- (void)startDrawingPaths
{
    //draw the first path
    pathsToDraw = 1;
    [view setNeedsDisplay];

    //schedule redraws once per second
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateView:) userInfo:nil repeats:YES];
}

- (void)updateView:(NSTimer*)timer
{
    //increment the path counter
    pathsDrawn++;

    //tell the view to update
    [view setNeedsDisplay];

    //if we've drawn all our paths, stop the timer
    if(pathsDrawn >= [myArrayOfBezierPaths count])
    {
        [timer invalidate];
    }
}

您的绘图方法如下所示:

- (void)drawRect:(CGRect)rect
{
    NSInteger i = 0;
    for (i = 0; i < pathsDrawn; i++) 
    {
        UIBezierPath * path = [myArrayOfBezierPaths objectAtIndex:i];
        path.lineWidth=3;
        [path stroke];
    }
}

就颜色而言,您正在创建路径,因此您可以将它们制作成您喜欢的任何颜色。

于 2011-06-26T08:21:59.340 回答