美文网首页iOS
有时候,滑动视图分页很难伺候

有时候,滑动视图分页很难伺候

作者: CYC666 | 来源:发表于2019-07-03 14:55 被阅读0次

    #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];

        });

    }

    相关文章

      网友评论

        本文标题:有时候,滑动视图分页很难伺候

        本文链接:https://www.haomeiwen.com/subject/pmgqhctx.html