带有下一个和上一个按钮的UICollectionView

时间:2015-04-24 07:28:59

标签: ios xcode uicollectionview uicollectionviewlayout

我有一个带有X-Number of Cells的UICollectionView,我有一个左侧和右侧的两个按钮,我为下一个和上一个按钮写了一个逻辑,当我点击下一个按钮时,两个单元格是来而不是一个。

- (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];
        }

    }

}

CellSizeCollectionViewCell的宽度*高度。

0 个答案:

没有答案