以编程方式滚动集合视图

Posted

技术标签:

【中文标题】以编程方式滚动集合视图【英文标题】:Scroll programmatically collection view 【发布时间】:2015-10-15 10:57:58 【问题描述】:

我有一个水平收藏视图。 我想要的是添加一个按钮,并在按下时自动滚动到下一个单元格。如何做到这一点?

我尝试了这种方法,但它不起作用:

- (IBAction)scrollCollection:(id)sender 

    NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];

    NSArray *indexPaths = [self.notesCollection indexPathsForSelectedItems];
    if ( indexPaths.count > 0 )
    
        NSIndexPath *oldIndexPath = indexPaths[0];
        NSInteger oldRow = oldIndexPath.row;
        newIndexPath = [NSIndexPath indexPathForRow:oldRow + 1 inSection:oldIndexPath.section];
    

还有这个:

- (IBAction)scrollCollection:(id)sender 

    UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)[self.notesCollection collectionViewLayout];
    [self.notesCollection setPagingEnabled:YES];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
     self.notesCollection.contentOffset = self.scrollingPoint;
    // Here you have to respond to user interactions or else the scrolling will not stop until it reaches the endPoint.
    if (CGPointEqualToPoint(self.scrollingPoint, self.endPoint)) 
        [self.scrollingTimer invalidate];
    
    // Going one pixel to the right.
    self.scrollingPoint = CGPointMake(self.scrollingPoint.x, self.scrollingPoint.y+1);

【问题讨论】:

[self.collectionView scrollToItemAtIndexPath: newIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:YES]; 【参考方案1】:

你可以试试这个

@property (nonatomic, assign) CGPoint scrollingPoint, endPoint;
@property (nonatomic, strong) NSTimer *scrollingTimer;
@synthesize scrollingPoint, endPoint;
@synthesize scrollingTimer;

- (void)scrollSlowly 
    // Set the point where the scrolling stops.
    self.endPoint = CGPointMake(0, 300);
    // Assuming that you are starting at 0, 0 and scrolling along the x-axis.
    self.scrollingPoint = CGPointMake(0, 0);
    // Change the timer interval for speed regulation. 
    self.scrollingTimer = [NSTimer scheduledTimerWithTimeInterval:0.015 target:self selector:@selector(scrollSlowlyToPoint) userInfo:nil repeats:YES];


- (void)scrollSlowlyToPoint 
    self.collectionView.contentOffset = self.scrollingPoint;
    // Here you have to respond to user interactions or else the scrolling will not stop until it reaches the endPoint.
    if (CGPointEqualToPoint(self.scrollingPoint, self.endPoint)) 
        [self.scrollingTimer invalidate];
    
    // Going one pixel to the right.
    self.scrollingPoint = CGPointMake(self.scrollingPoint.x, self.scrollingPoint.y+1);

或者你也可以使用这个代码。

NSInteger section = [self numberOfSectionsInCollectionView:collectionView] - 1;
NSInteger item = [self collectionView:collectionView numberOfItemsInSection:section] - 1;
NSIndexPath *lastIndexPath = [NSIndexPath indexPathForItem:item inSection:section];
[collectionView scrollToItemAtIndexPath:lastIndexPath atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];

【讨论】:

这是垂直滚动。横向呢? 设置 UICollectionViewScrollDirectionHorizo​​ntal [yourCollectionView setPagingEnabled:YES];

以上是关于以编程方式滚动集合视图的主要内容,如果未能解决你的问题,请参考以下文章

使用集合视图使视图控制器可滚动

同步动画以在表格视图中的集合视图中滚动

以编程方式获取集合视图部分在集合视图协议方法之外的高度

故事板集合视图拒绝在以编程方式创建的按钮前面

以编程方式将按钮添加到集合视图单元格

以编程方式将Tableview滚动到未来日期