【问题标题】:3 UIImageView animations with different intervals3个不同间隔的UIImageView动画
【发布时间】:2014-04-15 20:18:30
【问题描述】:

我有 3 张图片要按顺序向用户展示。从一个移动到另一个的时间以及图像对用户保持可见的时间不是恒定的,它会随着每个图像而变化。有人可以指出完成这项工作的最佳方法吗?感谢您的任何指示。

为了更清楚,我正在寻找 UIImageView.animationImages 之类的东西,然后开始动画。但是*我想控制每个图像的动画时间以及每个图像停留的时间在下一个图像动画之前就位。

【问题讨论】:

  • 时间间隔是多少?
  • 它因图像而异,例如。从 1->2 .3s 过渡,停留 0.5s 然后 2-> 3 .2s 过渡停留 0.4s,依此类推..

标签: ios objective-c image uiimageview uikit


【解决方案1】:

一个非常非常基本的解决方案可能是

- (void) showSequence{
//show image 1
//hide image 2, 3
double delay1 = 2.0;
double delay2 = 3.0;
double delay3 = 5.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay1 * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    //hide image 1
    //show image 2
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay2 * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        //hide image 2
        //show image 3
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay3 * NSEC_PER_SEC));
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            //hide image 3
            //all 3 images has been shown
        });
    });
});

}

由你来修改代码以防止重复

【讨论】:

  • 感谢您的回复..但这并没有动画..它显示/隐藏图像..
  • 你想要实现什么样的动画?
  • 当我从一张图像转到另一张图像时,我需要 1) 指定下一张图像的淡入持续时间。 2) 在我们移动到第三张图片之前等待一段时间。
【解决方案2】:

好的,我会怎么做。

您需要两个图像视图。

@property UIImageView *currentImageView;
@property UIImageView *nextImageView;

我想你会有一些模型来解决这个问题,包括等待时间和转换时间。

这样的……

@{
    @"imageName": @"image1",
    @"waitTime": @2.0,
    @"transitionTime": @0.4
}

然后将它们存储在一个类似...的数组中

@property NSArray *transitionImages;

这会让一切变得更容易。

现在你有了一个函数。

- (void)transitionToImageAtIndex:(NSInteger)index
{
    NSDictionary *transitionImage = self.transitionImages[index];

    self.nextImageView = [UIImage imageNamed:transitionImage[@"imageName"]];

    [UIView animateWithDuration:[transitionImage[@"transitionTime"] floatValue]
                     animations:^(){
                         // crossfade between the two views
                         self.nextImageView.alpha = 1.0;
                         self.currentImageView.alpha = 0.0;
                     }
                     completion:^(BOOL finished){
                         // swap the image views over
                         UIImageView *tempImageView = self.nextImageView;
                         self.nextImageView = self.currentImageView;
                         self.currentImageView = tempImageView;

                         // get the index of the next image
                         NSInteger nextIndex = index + 1;
                         if (nextIndex >= self.transitionImages.count) {
                             // reset to 0 if outside array bounds
                             nextIndex = 0;
                         }

                         // trigger the tradition to the next image recursively with correct delay
                         [self performSelector:@selector(transitionToImageAtIndex:) withObject:@(nextIndex) afterDelay:[transitionImage[@"waitTime"] floatValue]];
                     }];
}

这将递归地工作并永远持续下去。它不会阻止 UI 或任何东西,因此应用程序的其余部分将继续工作。

您还可以在此方法的开头添加一个检查以阻止它运行。这将停止循环。

【讨论】:

    猜你喜欢
    • 2012-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    • 2012-02-08
    • 1970-01-01
    • 2014-05-19
    相关资源
    最近更新 更多