在单击左侧和右侧按钮的同时向左和向右滚动CollectionView单元格

时间:2016-06-14 06:37:06

标签: ios objective-c collectionview

实际上我有UICollectionView这样的可见单元格

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    for (UICollectionViewCell *cell in [self.obj_CollectionView1 visibleCells]) {
        NSIndexPath *indexPath = [self.obj_CollectionView1 indexPathForCell:cell];
       lastIndex = [indexPath indexAtPosition:[indexPath length] - 1];

        if (lastIndex ==9) {
            [self.rightButton setHidden:YES];
        }
        if (lastIndex==0) {
            [self.leftButton setHidden:YES];
        }
        if (lastIndex>3) {
            [self.leftButton setHidden:NO];
        }
        if (lastIndex<6) {
            [self.rightButton setHidden:NO];
        }
    }
}

现在通过使用索引路径我想左右滚动,但我很困惑在newIndexPath中写什么

-(IBAction)RightButton:(id)sender
{
   if (lastIndex <4 && lastIndex >9) 
   {
     [_obj_CollectionView1 scrollToItemAtIndexPath:newIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
   }

}

,类似于左键

请帮忙 \

答案:

NSUInteger lastIndex;
NSIndexPath *indexPathOf;
NSMutableArray *indexContainer;
NSInteger  IndexVisibleCell,IndexVisible;
NSArray *labelcontent;
NSArray *reversedArray;

-(void)awakeFromNib
{

    _obj_CollectionView1.delegate = self;
    _obj_CollectionView1.dataSource = self;

    indexContainer = [[NSMutableArray alloc]initWithCapacity:15];
    labelcontent = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10"];


}




-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    InnerCollectionCell1 *innerCell= [collectionView dequeueReusableCellWithReuseIdentifier:@"innerCell1" forIndexPath:indexPath];

    innerCell.innerViewCell1.layer.borderColor = [UIColor brownColor].CGColor;

    innerCell.innerViewCell1.layer.borderWidth = 1;
    innerCell.innerLabelCell1.text = [labelcontent objectAtIndex:indexPath.item];

    return innerCell;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [labelcontent count];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{

    CGFloat Width = _obj_CollectionView1.frame.size.width-20;

    float cellWidth =Width / 3.0;

    CGSize size = CGSizeMake(cellWidth,_obj_CollectionView1.frame.size.height );

    return size;

}

-(IBAction)RightButton:(id)sender
{
       NSMutableArray *pat = [[ _obj_CollectionView1 indexPathsForVisibleItems] mutableCopy];

    [pat sortUsingComparator:^NSComparisonResult(id obj1, id obj2)
    {
        NSInteger r1 = [obj1 row];
        NSInteger r2 = [obj2 row];
        if (r1 > r2) {
            return (NSComparisonResult)NSOrderedDescending;
        }
        if (r1 < r2) {
            return (NSComparisonResult)NSOrderedAscending;
        }
        return (NSComparisonResult)NSOrderedSame;
    }];

    NSIndexPath *pathh = [NSIndexPath indexPathForRow:[(NSIndexPath *)[pat lastObject]item ]+1 inSection:0];

        [self.obj_CollectionView1 scrollToItemAtIndexPath:pathh atScrollPosition:UICollectionViewScrollPositionRight animated:YES];

}


-(IBAction)LeftButton:(id)sender
{

    NSMutableArray *pat = [[ _obj_CollectionView1 indexPathsForVisibleItems] mutableCopy];
      [pat sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        NSInteger r1 = [obj1 row];
        NSInteger r2 = [obj2 row];
        if (r1 > r2) {
            return (NSComparisonResult)NSOrderedDescending;
        }
        if (r1 < r2) {
            return (NSComparisonResult)NSOrderedAscending;
        }
        return (NSComparisonResult)NSOrderedSame;
    }];


    NSIndexPath *pathh = [NSIndexPath indexPathForRow:[(NSIndexPath *)[pat firstObject]item ]-1 inSection:0];

        [self.obj_CollectionView1 scrollToItemAtIndexPath:pathh atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];

}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{

      IndexVisible = [indexPath indexAtPosition:[indexPath length] - 1];

    if (IndexVisible==labelcontent.count-1)
    {
        [self.rightButton setHidden:YES];
    }
    if (IndexVisible<[labelcontent count]-3)
    {
        [self.rightButton setHidden:NO];
    }
    if (IndexVisible >2) {
        [self.leftButton setHidden:NO];
    }
    if (IndexVisible<1) {
        [self.leftButton setHidden:YES];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{

    for (UICollectionViewCell *cell in [self.obj_CollectionView1 visibleCells]) {
        indexPathOf = [self.obj_CollectionView1 indexPathForCell:cell];
        lastIndex = [indexPathOf indexAtPosition:[indexPathOf length] - 1];

        if (lastIndex==labelcontent.count-1)
        {
            [self.rightButton setHidden:YES];
        }
        if (lastIndex<[labelcontent count]-3)
        {
            [self.rightButton setHidden:NO];
        }
        if (lastIndex >2) {
            [self.leftButton setHidden:NO];
        }
        if (lastIndex<1) {
            [self.leftButton setHidden:YES];
        }

    }}

1 个答案:

答案 0 :(得分:0)

你的意思是说,应该一次最多可以看到3个项目,对吧?

如果是这样,请创建UICollectionViewFlowLayout属性,以便最多显示3个

- (void)calculateCollectionLayout
{
    [self.collectionView setContentInset:UIEdgeInsetsMake(kMargin, kMargin, kMargin, kMargin)];

    UICollectionViewFlowLayout *layout = ((UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout);
    [layout setMinimumInteritemSpacing:kPadding];
    [layout setMinimumLineSpacing:kMargin];
    CGFloat cellWidth = ((CGRectGetWidth(self.view.frame) - (kNumberOfColumns + 1) * kPadding) - (2 * kMargin)) / kNumberOfItemsToBeVisible ;
    [layout setItemSize:CGSizeMake(cellWidth,cellHeight)];

    [layout invalidateLayout];
}

在上面的代码 kNumberOfItemsToBeVisible 将为3

显示/隐藏左右按钮,根据总项目跟踪当前的内部项目

例如,currentPage将为3,并且最初将隐藏左侧(如果它是&lt; = 3隐藏左右按钮)。单击右键时,将其增加3.并检查它是否为&gt; = totalItems。如果隐藏右键。如果当前页面是1再次隐藏左按钮

相关问题