0

Can any one answer some sample code to animate uicollectionview vertically to the bottom.

dispatch_async(dispatch_get_main_queue(), ^{
            [self.clViewImages reloadData];
             scrolling collection view to bottom

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

This scrolls my collection view very fast. I want to soft animation section wise. help me find out.

thanks in advance

Vikram Sinha
  • 488
  • 8
  • 24
  • You are scrolling all the items in collection view in one go, that's why it's fast. So you can scroll to n items that are visible on screen (4-5 items) at a time and then next next n items and so on. In this way it'll we smooth. – Rahul Kumar Aug 23 '17 at 10:49
  • ok @RahulKumar let me do this – Vikram Sinha Aug 23 '17 at 11:08

2 Answers2

0

First of all, make sure you set your collection view datasource and delegate like below.

collectionView.datasource = self;
collectionView.delegate = self;

Next thing you have to check that collection Array should not nil.

If all conditions match then try reload your collection view.

[collectionView reloadData];
Neha Gupta
  • 519
  • 2
  • 12
  • Neha everything is working fine. the issue is I am reloading collection view at the same time animating it to bottom and it is going too fast. is there any solution for this ??? – Vikram Sinha Aug 23 '17 at 07:30
0

This helped me - :

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

//scroll with finite time interval
- (void)scrollSlowlyWithInterval:(NSNumber*)interval {
    CGFloat height = self.clViewImages.contentSize.height;
    animationStarted = YES;
    // Set the point where the scrolling stops.
    self.endPoint = CGPointMake(0, height-self.clViewImages.frame.size.height);
    // 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:[interval doubleValue] target:self selector:@selector(scrollSlowlyToPoint) userInfo:nil repeats:YES];
}

- (void)scrollSlowlyToPoint {
    [self.clViewImages setContentOffset:self.scrollingPoint animated:YES];
    // 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];
        animationStarted = NO;
    }
    // Going one pixel to the right.
    CGFloat height = [UIScreen mainScreen].bounds.size.height/2;
    self.scrollingPoint = CGPointMake(self.scrollingPoint.x, self.scrollingPoint.y+height);
}

// scrolling to bottom without default animation 
-(void)scrollToBottom{
    NSInteger section = [self.clViewImages numberOfSections] - 1;
    NSInteger items = [self.clViewImages numberOfItemsInSection:section] - 1;
    NSIndexPath *indxPath = [NSIndexPath indexPathForItem:items inSection:section];
    [self.clViewImages scrollToItemAtIndexPath:indxPath atScrollPosition:UICollectionViewScrollPositionBottom animated:YES];
}

-(void)reloadCollectionView{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.clViewImages reloadData];
        if(!animationStarted){
            [self scrollSlowlyWithInterval:[NSNumber numberWithDouble:0.50f]];
        }
});
}

thanks to Chris

Vikram Sinha
  • 488
  • 8
  • 24