beginAnimation with For Statement无法正常工作

时间:2011-11-09 22:08:15

标签: iphone ipad core-animation

更新代码 -

-(void)animateDot {
    dotMotion.center = (*doodlePoints)[0];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationRepeatCount:100];
    for(int i = 1; i < doodlePoints->size(); i++){
        dotMotion.center = (*doodlePoints)[i];
    }

    [UIView commitAnimations];
}

我有一个点矢量,我想让它从点到点做动画。我只是从第二次到最后一次到最后一次工作。以前没什么。 有什么想法吗?

所以我尝试这样做的方式不同。没有。 我的应用程序必须工作3.x所以我不能使用动画块。

3 个答案:

答案 0 :(得分:1)

您需要退出for循环,并在显示每个动画片段的每个线段后从此方法返回。只有在返回UI运行循环后才会更新UI。

在另一个延迟或委托回调方法中继续循环的每次迭代。将iterrator变量保存在方法之间的实例变量中。

答案 1 :(得分:1)

更好的尝试以下方法:

-(void) nextAnimation
{
    CGPoint point = [[self.animatePoints objectAtIndex:0] CGPointValue];
    [self.animatePoints removeObjectAtIndex:0];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationRepeatCount:100];
    dotMotion.center = point;
    [UIView commitAnimations];
}

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    if ([self.animatePoints count] > 0) {
        [self nextAnimation];
    }
}

-(void)animateDot {
    self.animatePoints = [NSMutableArray array];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    for(int i = 1; i < doodlePoints->size(); i++){
        [self.animatePoints addObject:[NSValue valueWithCGPoint:(*doodlePoints)[i]]];
    }
    [self nextAnimation];
}

这是在NSMutableArray属性中加载各个点的粗略示例,该属性在每个动画完成时以递增方式处理和清空。您可以轻松地使用doodlePoints数组的内部索引属性,但是如果您需要传入不同的点集合,我就是这样做的。在我的示例中,我们设置了动画委托和回调选择器,以便我们可以告诉每个动画完成。然后,我们将动画安排到下一个点,并将其从数组中删除。

答案 2 :(得分:-1)

你想要类似的东西(未经测试):

-(void)animateTo:(CGPoint) point {
    dotMotion.center = (*doodlePoints)[0];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationRepeatCount:100];
    dotMotion.center = point;
    [UIView commitAnimations];
}

-(void)animateDot {
    for(int i = 1; i < doodlePoints->size(); i++){
        [self animateTo:(*doodlePoints)[i]];
    }
}

虽然您可能不希望安排这些并发运行。您可能希望使用动画完成回调链接动画,并在每个动画完成后按顺序触发下一个动画。 已修改