【问题标题】:Bug after rotate, using iCarousel as scroll view旋转后的错误,使用 iCarousel 作为滚动视图
【发布时间】:2016-03-16 12:30:11
【问题描述】:

我使用 iCarousel lib 作为我的应用程序的滚动视图。该库的目的是管理自动滚动图像。图像设置为 iCarousel 类的边界(实际上是UIView。最初加载时它工作正常,但是当我旋转设备时,它会输出奇怪的错误。例如,如果我从纵向滑动到横向,只需要几分之一秒我们可以看到以下内容:

然后:

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

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

- (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];
    }

}

如何修复该错误?显然我不想在新图像之间加载未来和旧图像。

【问题讨论】:

  • 我不确定它是iCarousel 中的feature 还是bug,但我可以告诉你它的实现和行为非常- 非常奇怪(主要是错误)——我们在方向改变后为解决问题所做的工作,我们每次都会在实际的 iCarousel 实例上调用 –reloadData 方法。
  • @holex 我确实打电话给 [car reloadData];在 - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {,但不幸的是问题并没有消失。
  • 如果您在实际轮换发生之前重新加载数据,这对您并没有真正的帮助;您需要之后进行。
  • @holex 好的,我试试,谢谢!:)
  • @EvgeniyKleban 我对 iCarousel 也有同样的问题。当我在屏幕旋转后运行 reloadData 时,我可以在一秒钟内看到旧尺寸的轮播。它看起来不那么漂亮。你能告诉我你是怎么解决这个问题的吗?也许你应该添加你对这个问题的答案。

标签: ios objective-c icarousel


【解决方案1】:

语言Swift

按如下方式重新加载轮播视图:

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

【讨论】:

  • 完美。在我的情况下,carouselItemWidth: 的 Frame 而不是 Bounds yourCarouselView.frame.width 是罪魁祸首。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-15
  • 2017-07-31
  • 2013-12-25
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
相关资源
最近更新 更多