Open MISSAJJ opened 8 years ago
如果适配缩放比例获得最终控件W为 336.375000 CGfloat sliderW = 336.375000;
SDCycleScrollView *cycleScrollView = [cycleScrollViewWithFrame:CGRectMake(0,0,sliderW,sliderH) delegate:self placeholderImage: placeholderImage:placeholderImage];
请看打印数据:
轮播几次后,_mainView.contentOffset.x 就一直为 35319.333333, 导致无限轮播停止运行
2016-02-18 17:36:24.766 shtrip[1136:295048] _mainView.contentOffset.x33637.666667====_flowLayout.itemSize.width336.375000 2016-02-18 17:36:26.766 shtrip[1136:295048] _mainView.contentOffset.x33974.000000====_flowLayout.itemSize.width336.375000 2016-02-18 17:36:28.766 shtrip[1136:295048] _mainView.contentOffset.x34310.333333====_flowLayout.itemSize.width336.375000 2016-02-18 17:36:30.766 shtrip[1136:295048] _mainView.contentOffset.x34646.666667====_flowLayout.itemSize.width336.375000 2016-02-18 17:36:32.766 shtrip[1136:295048] _mainView.contentOffset.x34983.000000====_flowLayout.itemSize.width336.375000 2016-02-18 17:36:34.766 shtrip[1136:295048] _mainView.contentOffset.x35319.333333====_flowLayout.itemSize.width336.375000 2016-02-18 17:36:36.766 shtrip[1136:295048] _mainView.contentOffset.x35319.333333====_flowLayout.itemSize.width336.375000 2016-02-18 17:36:38.766 shtrip[1136:295048] _mainView.contentOffset.x35319.333333====_flowLayout.itemSize.width336.375000 2016-02-18 17:36:40.766 shtrip[1136:295048] _mainView.contentOffset.x35319.333333====_flowLayout.itemSize.width336.375000 2016-02-18 17:36:42.766 shtrip[1136:295048] _mainView.contentOffset.x35319.333333====_flowLayout.itemSize.width336.375000 2016-02-18 17:36:44.766 shtrip[1136:295048] _mainView.contentOffset.x35319.333333====_flowLayout.itemSize.width336.375000 2016-02-18 17:36:46.766 shtrip[1136:295048] _mainView.contentOffset.x35319.333333====_flowLayout.itemSize.width336.375000 2016-02-18 17:36:48.766 shtrip[1136:295048] _mainView.contentOffset.x35319.333333====_flowLayout.itemSize.width336.375000
所以先强转为int再算出的int currentIndex就不会错了 int currentIndex = (int)_mainView.contentOffset.x / (int)_flowLayout.itemSize.width;
(void)automaticScroll { if (0 == _totalItemsCount) return; NSLog(@"_mainView.contentOffset.x%d====_flowLayout.itemSize.width%d",(int)_mainView.contentOffset.x,(int)_flowLayout.itemSize.width);
//此处为MISSAJJ修改bug //原因:在做IOS多屏幕适配时,算出的currentIndex会出错,会导致出现轮播几次就被停止了
//解释:我们有的时候会通过以iphone5屏幕(kDeviceWidth/320,kDeviceHeight/568)为基础标准的尺寸比例,去缩放IPhone6,6plus的尺寸 //而经过屏幕适配后所获得的控件CGfloat W宽有的时候会出现类似254.66666这样的float浮点,_mainView.contentOffset.x也为float浮点, //所以将两者都先强转为int,再算currentIndex就不再出错了
int currentIndex = (int)_mainView.contentOffset.x / (int)_flowLayout.itemSize.width; //int currentIndex = _mainView.contentOffset.x /_flowLayout.itemSize.width; //此句为原作者的代码 int targetIndex = currentIndex + 1; if (targetIndex == _totalItemsCount) { if (self.infiniteLoop) { targetIndex = _totalItemsCount * 0.5; }else{ return; } [_mainView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:targetIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO]; } [_mainView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:targetIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES]; }