0

我正在尝试创建校准视图!我有 12 个校准点作为 UIImageViews。现在我想显示每个点 5 秒。所以整个校准时间是1分钟。ImageViews 的 Alpha 设置为 0.0!在 UI 动画中,我想一个接一个地将 Alpha 设置为 1.0,每个点仅持续 5 秒。
到目前为止,我做到了(见代码)!但这会让我(淡入)在 5 秒后 5 秒一次全部 12 个点!我如何使用 NSTimer 和 UI 动画一个接一个地解决这个问题?谢谢

-(id)init{
      _calibrationViewArray = [[NSMutableArray alloc]init];
      _calibrationPoint1 = [[UIImageView alloc]initWithFrame:(CGRectMake(115.5,113.0,25.0,25.0))];
      _calibrationPoint1.backgroundColor = [UIColor redColor];
      _calibrationPoint1.alpha = 0.0;
      [self addSubview:_calibrationPoint1];
      [_calibrationViewArray addObject:_calibrationPoint1];

      [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
}

-(void)onTimer{

    for (int i = 0; i < [_calibrationViewArray count]; i++) {

        UIImageView* currentCalibrationPoint = [_calibrationViewArray objectAtIndex:i];

        [UIView beginAnimations:@"Calibration" context:nil];
        [UIView setAnimationDuration:5.0];

        // Make the animatable changes.
        currentCalibrationPoint.alpha = 1.0;


        // Commit the changes and perform the animation.
        [UIView commitAnimations];
    }

}
4

3 回答 3

2

在你的类中声明一个变量为:

int cont;

需要时初始化变量:

cont=0;

将您的代码更改为:

-(void)onTimer{

    //Loop is not needed, with the timer and counter is enough


    UIImageView* currentCalibrationPoint = [_calibrationViewArray objectAtIndex:cont];

    [UIView beginAnimations:@"Calibration" context:nil];
    [UIView setAnimationDuration:5.0];

    // Make the animatable changes.
    currentCalibrationPoint.alpha = 1.0;


    // Commit the changes and perform the animation.
    [UIView commitAnimations];

    if(cont>0){
         //Hide previous view (here you can make another animation, instead of changing alpha right away)
         [_calibrationViewArray objectAtIndex:cont-1].alpha = 0.0;
    }
    cont++;//+1

}
于 2013-09-11T15:01:43.760 回答
1

在您的onTimer方法中,您不应该遍历所有图像视图,您应该有一个currentIndex或类似的变量,这样您就可以获取下一个图像视图并单独在其上运行动画。

于 2013-09-11T15:03:21.050 回答
0

根本不要使用计时器。

使用:

  [UIView animateWithDuration: animations: completion:]

方法。

在动画块中淡化一个点,然后在完成块中再次运行动画。

于 2013-09-11T15:02:26.423 回答