重复UIView动画循环中的延迟吗?

问题描述 投票:5回答:3

我正在尝试对这样的视图不透明度进行自定义动画:

  1. 延迟5秒钟; (视图将保持不透明5秒钟)
  2. 从不透明度值1到0进行动画处理;
  3. 延迟5秒钟; (视图将保持透明5秒钟)
  4. 从不透明度值0到1进行动画处理;

我想无限地重复步骤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,.....

ios uiview delay uiviewanimation repeat
3个回答
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
投票

@@ 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动画序列加在一起。


1
投票

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

Swift 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

© www.soinside.com 2019 - 2024. All rights reserved.