【问题标题】:How to change to another image every 5 seconds in UIView?如何在 UIView 中每 5 秒更改一次图像?
【发布时间】:2017-11-11 20:22:06
【问题描述】:

我目前有一个从我的站点获取图像的横幅,我可以左右滚动横幅以获取下一张或上一张图片。但是,我还希望它每 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];

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

【问题讨论】:

    标签: ios objective-c xcode uiview nstimer


    【解决方案1】:

    ScrollView 有一个属性

     setContentOffset:animated:
    

    在 NSTimer 函数中使用此属性查看效果。关注此thread。它包含如何制作这个的逐步过程。

    或者。

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

    UIPageVC 有这样的方法。

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

    它期望提供数据。然后就可以使用 NSTimer 一定时间给图像了。

    【讨论】:

    • 谢谢。我现在已经让它工作了。我在 imageView 中添加了一个 NStimer,现在它每 5 秒触发一次。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-16
    • 1970-01-01
    • 2013-02-06
    • 1970-01-01
    相关资源
    最近更新 更多