【发布时间】: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];
标签: ios objective-c scroll uicollectionview