2015-10-15 75 views
0

我有一個水平收集視圖。 我想要的是添加一個按鈕,當按下時自動滾動到下一個單元格。任何ideea如何做到這一點?以編程方式滾動收集視圖

我嘗試這種方法,但它不工作:

- (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); 

}

+2

[self.collectionView scrollToItemAtIndexPath:newIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredVertically動畫:YES]; – pkc456

+0

這不是幫助我 –

回答

0

你可以試試這個

@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]; 
+0

這是垂直滾動。水平怎麼樣? –

+0

設置UICollectionViewScrollDirectionHorizo​​ntal – vijay

+0

它已經設置,第二種方法不工作。 –

相關問題