Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions SDCycleScrollView/Lib/SDCycleScrollView/SDCycleScrollView.m
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,19 @@ - (void)setupMainView
_mainView = mainView;
}

- (void)scrollToItemAtIndex:(NSInteger)index animated:(BOOL)animated {

//在ipx上宽度为小数的时候会出现错乱的情况,所以不能使用
// [_mainView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];

//下面方法这样避免了轮播view宽度为小数的时候轮播错乱的情况
if (_flowLayout.scrollDirection == UICollectionViewScrollDirectionHorizontal) {
[_mainView setContentOffset:CGPointMake(index*_flowLayout.itemSize.width, 0) animated:animated];
}else{
[_mainView setContentOffset:CGPointMake(0, index*_flowLayout.itemSize.width) animated:animated];
}

}

#pragma mark - properties

Expand Down Expand Up @@ -442,11 +455,11 @@ - (void)scrollToIndex:(int)targetIndex
if (targetIndex >= _totalItemsCount) {
if (self.infiniteLoop) {
targetIndex = _totalItemsCount * 0.5;
[_mainView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:targetIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
[self scrollToItemAtIndex:targetIndex animated:NO];
}
return;
}
[_mainView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:targetIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
[self scrollToItemAtIndex:targetIndex animated:YES];
}

- (int)currentIndex
Expand Down Expand Up @@ -498,7 +511,7 @@ - (void)layoutSubviews
}else{
targetIndex = 0;
}
[_mainView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:targetIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
[self scrollToItemAtIndex:targetIndex animated:NO];
}

CGSize size = CGSizeZero;
Expand Down Expand Up @@ -561,7 +574,7 @@ - (void)adjustWhenControllerViewWillAppera
{
long targetIndex = [self currentIndex];
if (targetIndex < _totalItemsCount) {
[_mainView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:targetIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
[self scrollToItemAtIndex:targetIndex animated:NO];
}
}

Expand Down
4 changes: 3 additions & 1 deletion SDCycleScrollView/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ - (void)viewDidLoad {
// >>>>>>>>>>>>>>>>>>>>>>>>> demo轮播图2 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

// 网络加载 --- 创建带标题的图片轮播器
SDCycleScrollView *cycleScrollView2 = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 280, w, 180) delegate:self placeholderImage:[UIImage imageNamed:@"placeholder"]];
CGFloat width = 22.08; //在ipX上当滑动的轮播的View的宽度为小数时,轮播错误,会出现播放前两个,后面不播的情况
// CGFloat width = 22; //当轮播的view宽度为整数的时候,正常播放
SDCycleScrollView *cycleScrollView2 = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(width, 280, w-width*2, 180) delegate:self placeholderImage:[UIImage imageNamed:@"placeholder"]];

cycleScrollView2.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
cycleScrollView2.titlesGroup = titles;
Expand Down