如何在 UIView 中每 5 秒更改为另一个图像?

How to change to another image every 5 seconds in UIView?

我目前有一个横幅可以从我的网站获取图片,我可以左右滚动横幅以获取下一张或上一张图片。但是,我还希望它每 5 秒自动更改图像。

我尝试将这些代码添加到 imageView,但横幅变成空的,不显示任何图像。

imageView.animateImages = delegate.bannerDetailsArray;
imageView.animationDuration = 5.0;
imageView.animationRepeatCount = 0;
[imageview startAnimating];

我猜数组是问题所在,但我目前不知道如何解决它。

谁能切碎一些灯光如何使横幅图像每5秒变化一次?

-(void) bannerSettings
{
bannerScrollView = [UIScrollView new];
[bannerScrollView setBackgroundColor:BackGroundColor];
[bannerScrollView setFrame:CGRectMake(0,0,delegate.windowWidth, [self     imageWithImage:delegate.windowWidth maxHeight:200])];
[bannerScrollView setPagingEnabled:YES];
[bannerScrollView   setContentSize:CGSizeMake([delegate.bannerDetailsArray count]*delegate.windowWidth, 0)];

NSLog(@"%@",delegate.bannerDetailsArray);

for(int i=0;i<[delegate.bannerDetailsArray count];i++)
{
    UIImageView * imageView = [UIImageView new];
    [imageView setFrame:CGRectMake(i*delegate.windowWidth,0,delegate.windowWidth,[self imageWithImage:delegate.windowWidth maxHeight:200])];
    [imageView sd_setImageWithURL:[NSURL URLWithString:[[delegate.bannerDetailsArray objectAtIndex:i]objectForKey:@"bannerImage"]]];
    [imageView setContentMode:UIViewContentModeScaleAspectFill];
    [imageView.layer setMasksToBounds:YES];
    [imageView setUserInteractionEnabled:YES];
    [imageView setTag:i];

    //imageView.animateImages = delegate.bannerDetailsArray;
    //imageView.animationDuration = 3.0;
    //imageView.animationRepeatCount = 0;
    //[imageview startAnimating];


    [bannerScrollView addSubview:imageView];

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bannerGestureTapped:)];
    [imageView addGestureRecognizer:singleTap];

*当动画部分被忽略时,上面的代码工作正常。 谢谢

ScrollView 有一个 属性

 setContentOffset:animated:

在 NSTimer 函数中使用这个 属性 来查看 effect.Follow 这个 thread。它包含如何制作这个的分步过程。

或.

您可以使用 UIPageViewController 和 NStimer 组合,如 this 流行线程。

UIPageVC 有这样的方法。

- (void)setViewControllers:(NSArray *)viewControllers
                 direction:(UIPageViewControllerNavigationDirection)direction
                  animated:(BOOL)animated
                completion:(void (^)(BOOL finished))completion

它期待一个数据传递。然后就可以使用NSTimer一段时间给图像了。