如何在UIView中每5秒更换一次另一张图片?

时间:2017-06-09 18:03:12

标签: ios objective-c xcode uiview nstimer

我目前有一个从我的网站获取图片的横幅,我可以滚动左边的横幅和获得下一张或上一张图片的权利。但是,我也希望它每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];

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

1 个答案:

答案 0 :(得分:0)

&lt; p&gt; ScrollView有一个属性&lt; / p&gt; &LT;预&GT;&LT;代码&GT; setContentOffset:动画: &LT; /代码&GT;&LT; /预&GT; &lt; p&gt;在NSTimer函数中使用此属性以查看效果。按此&lt; a href =“https://stackoverflow.com/questions/13258938/automatic-scrolling-scrollview”&gt; thread&lt; / a&gt;。它包含如何进行此操作的分步过程。&lt; / p&gt; &LT p为H.;&LT;强&gt;或&LT; /强&GT;&LT; / p为H. &lt; p&gt;您可以使用UIPageViewController和NStimer组合,例如&lt; a href =“https://stackoverflow.com/questions/26668536/uipageviewcontroller-automatic-scrolling-in-ios”&gt; this&lt; / a&gt;流行的线程。 &LT; / p为H. &lt; p&gt; UIPageVC有这样的方法。&lt; / p&gt; &lt; pre&gt;&lt; code&gt; - (void)setViewControllers:(NSArray *)viewControllers                  方向:(UIPageViewControllerNavigationDirection)方向                   动画:(BOOL)动画                 完成:(void(^)(BOOL完成))完成 &LT; /代码&GT;&LT; /预&GT; &lt; p&gt;它希望提供数据。然后,您可以使用NSTimer一段时间来提供图像。&lt; / p&gt;