HHuckebein / HorizontalPicker

A similar to UIPickerView but horizontal picker view.
https://github.com/HHuckebein/HorizontalPicker
MIT License
49 stars 24 forks source link

Scrolling all the way right reset to 0 index, expected is last element #5

Closed sanojKashyap closed 8 years ago

sanojKashyap commented 10 years ago

I am facing issue, in which if scroll all the way to right will reset to 0 index which should be last index. Please, help me out.

szemian commented 9 years ago

I had a similar issue like yours when my HorizontalPicker is very narrow.

I managed to stop the behaviour by slightly modifying HPCollectionVC's indexForCenterCellFromCollectionView method:

- (NSInteger)indexForCenterCellFromCollectionView:(UICollectionView *)collectionView
{
    NSInteger index = 0;

    // check if baseAdjustments where made in HorizontalPickerView
    if (collectionView.contentOffset.x) {
        CGPoint point = collectionView.contentOffset;
        point.x += collectionView.frame.size.width / 2;
        // modified min of point.x
        point.x  = MIN(collectionView.contentSize.width - 1.0f, point.x);
        point.y += collectionView.frame.size.height / 2;
        index = [collectionView indexPathForItemAtPoint:point].row;
    }

    return index;
}

Hope it helps.