Ios 我想通过一系列的字符串来制作动画

Ios 我想通过一系列的字符串来制作动画,ios,animation,nsarray,Ios,Animation,Nsarray,我有一个字符串数组,我想用一个简单的淡入/淡出动画来设置动画 ---------------------------------------------------------------------------------- | fade in | String 1 ---> displayed in UILabel | fade out ---------------------------------------------------------------

我有一个字符串数组,我想用一个简单的淡入/淡出动画来设置动画

----------------------------------------------------------------------------------
|     fade in
|     String 1 ---> displayed in UILabel
|     fade out
----------------------------------------------------------------------------------

----------------------------------------------------------------------------------
|     fade in
|     String 2
|     fade out
----------------------------------------------------------------------------------
等等

每一个都保持它的位置大约5秒钟,然后继续到阵列中的下一个项目。我不知道从哪里开始。。。我知道如何执行动画,但我不太确定如何在每次迭代中暂停5秒(非线程阻塞)的字符串数组中设置动画。有什么想法吗

根据接受的答案更新我的解决方案

- (void)startTimer {
    arrayIndex = 0;

    NSArray *myarray = [alerts_dict allValues]; 
    breakingLabel.text = [myarray objectAtIndex:frameCount];

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [UIView setAnimationDuration:1];
    breakingLabel.alpha = 1;
    [UIView commitAnimations];

    arrayIndex++;
    pauseTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(myFunction) userInfo:nil repeats:YES];
}

- (void)myFunction {    

   [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^() {
       breakingLabel.alpha = 0;
   }
   completion:^(BOOL finished) {
       NSArray *myarray = [alerts_dict allValues]; 

       if (arrayIndex == [myarray count])
           arrayIndex = 0;

       breakingLabel.text = [myarray objectAtIndex:arrayIndex];

       arrayIndex++;

       [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^() {
           breakingLabel.alpha = 1;
       } completion:nil];
   }];
}

使用块动画可以非常轻松地执行此操作:

  • 将标签的
    alpha
    动画设置为0.0
  • 完成时:将其
    text
    设置为新字符串,并将其
    alpha
    设置为1.0(或原始值,无论它是什么)
  • NSTimer
    timeout回调可以重复调用此例程。只需实例化一个间隔为5.0的计时器,指定回调并计划它在运行循环中运行,如下所示:

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self
                      selector:@selector(timerFired:) userInfo:nil repeats:YES];
    
    (...)
    
    - (void)timerFired:(NSTimer *)timer
    {
        // call the method to update the label with the next string in the array
        // or invalidate the timer if you've reached the end of array
    }
    

    我会这样做:

    在viewDidLoad:中使用以下代码将字符串的alpha设置为0

    for (UILabel *label in labelArray) {
         label.alpha = 0;
    }
    
    然后在ViewWillDisplay:或ViewDidDisplay:中放入此代码(取决于您想要什么,是否希望在视图出现后立即启动)

    当然,您可以调整此方法,使其适合您的需要(例如,如果您希望使其更通用,请在其中添加一个duration参数)

    for (int i = 0; i < 5; i++) {
         [self animateView:[labelArray objectAtIndex:i] withDelay:i * 5];
    }
    
    - (void)animateView:(UIView *)view withDelay:(float)delay {
         [UIView animateWithDuration:1 delay:delay options:UIViewAnimationOptionCurveEaseInOut animations:^{
              view.alpha = 1.0;
         } completion:^(BOOL completed) {
              [UIView animateWithDuration:1 animations:^{
                   view.alpha = 0.0];
              }];
         }];
    }