在UIView动画循环中重复延迟?

时间:2013-06-05 07:10:05

标签: ios uiview delay uiviewanimation repeat

我正在尝试对视图不透明度执行自定义动画,如下所示:

  1. 延迟5秒; (视图将保持不透明状态5秒)
  2. 从不透明度值1动画到0;
  3. 延迟5秒; (视图将保持透明5秒钟)
  4. 从不透明度值0到1的动画;
  5. 我想无限期地重复步骤1到4:1,2,3,4,1,2,3,4 ......

    以下是我的尝试:

    [UIView animateWithDuration:1
                          delay:5
                        options:UIViewAnimationOptionAutoreverse|UIViewAnimationOptionRepeat|UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         self.imageView.layer.opacity = 0;
                     }
                     completion:nil
     ];
    

    但延迟只在开始时出现一次,我最终得到的是:

    1,2,4,2,4,2,4 .......

3 个答案:

答案 0 :(得分:8)

我遇到了同样的问题。我用这种方式解决了NSTimer

的问题
[NSTimer scheduledTimerWithTimeInterval: 2
                                     target: self
                                   selector:@selector(moveImage: )
                                   userInfo: nil repeats:YES];


}



-(void) moveImage: (NSTimer*)timer {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:2];

答案 1 :(得分:1)

@ user1980004感谢您对NSTimer的提示。我只是发布我的问题的完整工作代码:

// interface
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (strong, nonatomic) NSTimer *animationTimer;

-(void) viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self glowAnimation]; // Timer fires after a delay, so I need to fire animation for the first time

    // The interval value is determined by the sum of delay and duration for both the forward and reverse animation in glowAnimation function;
    self.animationTimer = [NSTimer scheduledTimerWithTimeInterval:12 target:self selector:@selector(glowAnimation) userInfo:nil repeats:YES];
}

-(void) viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

    if (self.animationTimer){ 
        [self.animationTimer invalidate]; // Cancel the timer schedule
        self.animationTimer = nil;
    }
}

-(void) glowAnimation{
    // Forward and reverse animation are chained with delay in each.
    [UIView animateWithDuration:1
                          delay:5
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         self.imageView.layer.opacity = 0;
                     }
                     completion:^(BOOL finished){
                         [UIView animateWithDuration:1
                                               delay:5
                                             options:UIViewAnimationOptionCurveEaseInOut
                                          animations:^{
                                              self.imageView.layer.opacity = 1;
                                          }
                                          completion:nil                          ];
                     }
     ];
}

可以实现问题中的1,2,3,4,1,2,3,4动画序列。

答案 2 :(得分:1)

这是一个古老的问题,但变化不大,它出现在我的搜索中,因此我认为这是一个更好的建议。 NSTimer是不必要的,使用UIView上的关键帧动画方法可以达到相同的效果。

快速5

// durations in seconds [ pause 5s | fade out 1s | pause 5s | fade in 1s] = 12s total
let fadeDuration: Double = 1
let delayDuration: Double = 5
let totalDuration: Double = delayDuration + fadeDuration + delayDuration + fadeDuration

// convert to relative times for key frames
let fadeRelativeDuration: Double = fadeDuration / totalDuration
let firstStartRelativeTime: Double = delayDuration / totalDuration
let secondStartRelativeTime: Double = (delayDuration + fadeDuration + delayDuration) / totalDuration

UIView.animateKeyframes(withDuration: totalDuration, delay: 0, options: [.repeat], animations: {
    UIView.addKeyframe(withRelativeStartTime: firstStartRelativeTime, relativeDuration: fadeRelativeDuration) {
        self.imageView.layer.opacity = 0
    }
    UIView.addKeyframe(withRelativeStartTime: secondStartRelativeTime, relativeDuration: fadeRelativeDuration) {
        self.imageView.layer.opacity = 1
    }
})

应用程序后台运行后,您仍然需要重新启动动画,例如通过在视图或视图控制器中观察UIApplicationWillEnterForegroundNotification