0

I have an UICollectionView with X-Number of Cells, and i have two buttons one left side an right side, I have written a logic for next and previous buttons, when I tap on next button two cells are coming instead of one.

- (IBAction)rightArrowAction:(id)sender
{
    [sender setTag:1];

    [self moveCell:sender];
}

- (IBAction)leftArrowAction:(id)sender {

    [sender setTag:0];

    [self moveCell:sender];
}

-(void)moveCell:(UIButton*)sender
{
    UIButton *senderBtn=(UIButton*)sender;
    if (senderBtn.tag==1)
    {
        if (self.widgetArray.count-1>_itemCount)
        {
            _itemCount=_itemCount+1;
        }
    }
    else
    {
        _itemCount=_itemCount-1;
    }
    NSIndexPath *iPath = [NSIndexPath indexPathForItem:_itemCount
                                             inSection:0];
    [self.productCollectionView scrollToItemAtIndexPath:iPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];

    [self handleArrowHidden:_itemCount];
}


- (void)scrollViewDidScroll:(UIScrollView *)sender
{
    if (sender==self.productCollectionView)
    {
        UICollectionView * col= (UICollectionView*)sender;
        _itemCount=col.contentOffset.x/self.cellSize.width;
        float trailingEdge = col.contentOffset.x +  col.frame.size.width;

        if (self.cellSize.width*[_widgetArray count]<=self.frame.size.width) {
            [_leftArrowView setHidden:YES];
            [_rightArrowView setHidden:YES];

        }
        else if (col.contentOffset.x<self.cellSize.width/2)
        {
            [_leftArrowView setHidden:YES];
            [_rightArrowView setHidden:NO];

        }
        else if (trailingEdge >= col.contentSize.width)
        {
            [_leftArrowView setHidden:NO];
            [_rightArrowView setHidden:YES];

        }
        else{
            [_leftArrowView setHidden:NO];
            [_rightArrowView setHidden:NO];
        }

    }

}

CellSize is the CollectionViewCells width*height.

kidshaw
  • 3,423
  • 2
  • 16
  • 28
Harish
  • 2,496
  • 4
  • 24
  • 48

0 Answers0