旋转后的错误,使用iCarousel作为滚动视图

时间:2016-03-16 12:30:12

标签: ios objective-c icarousel

我使用iCarousel lib作为我的应用程序的滚动视图。该库的目的是管理自动滚动图像。图像被设置为iCarousel类的边界(实际上是UIView。它在最初加载时工作正常,但是当我旋转设备时,它输出奇怪的bug。例如,如果我从纵向滑动到横向,对于分数我们可以看到以下内容: enter image description here

然后:enter image description here

出于某种原因,我们看到“旧”图片

以下是我如何声明显示视图的主要方法:

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view {



    NewScrollerItemForIPad *item;



    NewsItemMdl *mdl = self.viewModel.arrNews[index];
    if(mdl){

        /* Check for orientation */
        BOOL _isPortrait;

        if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)){
            _isPortrait = NO;
        } else {
            _isPortrait = YES;
        }

        if (self.gotNews){

        if(!item){
            item = [[NewScrollerItemForIPad alloc] initWithFrame:car.bounds andOrientation:_isPortrait]; 
        }
        [item bindViewModel:mdl];
        }
    }
    return item;

}

自动滚动方法:

-(void)runMethod{

    if(self.sliderCount== (self.pageControl.numberOfPages-1)){

        NSLog(@"Here we call");
        self.pageControl.currentPage = 0;
        self.sliderCount = 0;

        [car scrollToItemAtIndex:self.sliderCount animated:YES];

    }else{
        NSLog(@"And here we call");
        self.sliderCount++;
        self.pageControl.currentPage ++;
        [car scrollToItemAtIndex:self.sliderCount animated:YES];
    }

}

如何修复该错误?显然,我不希望未来和旧图像被加载到新的。

1 个答案:

答案 0 :(得分:0)

语言快捷

重新加载“轮播”视图,如下所示:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    coordinator.animate(alongsideTransition: { _ in
        self.yourCarouselView.reloadData()
    }, completion: nil)
}

使用以下委托方法确定项目宽度:

func carouselItemWidth(_ carousel: iCarousel) -> CGFloat {
    return yourCarouselView.bounds.width //Whatever the size you want to set
}

还有

yourCarouselView.isPagingEnabled = true