#pragma mark - 手动配置cell居中
- (void)fixCellToCenter {
//最小滚动距离
floatdragMiniDistance =SCREEN_WIDTH/20.0f;
if(_dragStartX- _dragEndX>= dragMiniDistance) {
_selectedIndex -= 1;//向右
}elseif(_dragEndX- _dragStartX>= dragMiniDistance){
_selectedIndex += 1;//向左
}
NSIntegermaxIndex = [_soreceListViewnumberOfItemsInSection:0] -1;
_selectedIndex = _selectedIndex <= 0 ? 0 : _selectedIndex;
_selectedIndex = _selectedIndex >= maxIndex ? maxIndex : _selectedIndex;
[self scrollToCenter];
}
//滚动到中间
- (void)scrollToCenter {
[_soreceListView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:_selectedIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
}
//手指拖动开始
- (void)scrollViewWillBeginDragging:(UIScrollView*)scrollView {
_dragStartX= scrollView.contentOffset.x;
}
//手指拖动停止
- (void)scrollViewDidEndDragging:(UIScrollView*)scrollView willDecelerate:(BOOL)decelerate {
_dragEndX= scrollView.contentOffset.x;
dispatch_async(dispatch_get_main_queue(), ^{
[self fixCellToCenter];
});
}
网友评论