繁体   English   中英

如何一个接一个显示图像

[英]How do you display images one after the other

我目前正在遍历一个列表,并根据该元素的条件,我想显示两个图像之一。 效果很好,但是我该如何在显示图像之间暂停一下。 我试过使用:

for(
   ...
   [myView addSubview:myImage]
   sleep(1)
   ...
)

每次检查之后,但出于某种原因,它会等待功能结束,然后再显示任何图像。

有谁引起这是什么原因以及如何解决它?

NSArray *imageArray = [NSArray arrayWithObjects:image1, image2, imageN, nil];
UIImageView *imageView = [[UIImageView alloc] init];
imageView.animationImages = imageArray;
imageView.animationDuration = [imageArray count]*3;
imageView.animationRepeatCount = 0;
[imageView startAnimating];

您不能在此处使用sleep() ,因为它会阻塞正在更新UI的线程。

您可以做的是调用另一个方法来设置第二个图像:

for(
   ...
   [myView addSubview:myImage]
   // schedule the method.
   [myView performSelector:@selector(addSubview:) withObject:mySecondImage afterDelay:1.0f];
   ...
)

您可以将元素的Alpha /可见性设置为0.0f / hidden,并使用带有延迟的动画块来显示视图。 通过将持续时间设置为大于0的值,在过程中获得精美的动画;-)

[UIView animateWithDuration: 0.0
                      delay: 1.0
                    options: UIViewAnimationCurveEaseOut
                 animations: ^{
                         myView.alpha = 1.0f;
                     }
                 completion:^(BOOL finished){}];

您可以创建一个函数来显示图像以“延迟执行”:

-(void)displayImage {
    [...get the image...]

    [myView addSubview:myImage];

    [self performSelector:@selector(displayImage) withObject:nil afterDelay:1.0];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM