Vignesh
Vignesh

Reputation: 644

iOS - TableView willDisplayCell animation only happen when user scrolling down not to the top

Hi am going to animate the table view cell when the user scrolling down if the user scroll to the top the will normally move with out animation.

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cells forRowAtIndexPath:(NSIndexPath *)indexPath{
    cells.alpha = 0.0;
    [UIView animateWithDuration:0.4 animations:^{
        cells.alpha = 1.0;
    }];
}

i tried like this but the animation happen all the time

Upvotes: 1

Views: 2369

Answers (2)

AshokPolu
AshokPolu

Reputation: 645

Create a Custom cell with Bool Property "isAnimated". By Default "isAnimated" value is "NO".

and do code changes in willDisplayCell:(UITableViewCell *)cell method.

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
//Typecast UITableViewCell to CustomCell. ex: CustomCell *objCell = (CustomCell *)cell
// check isAnimated or not 
   if(cell.isAnimated) return;
   cell.alpha = 0.0;
   [UIView animateWithDuration:0.4 animations:^{
      cell.alpha = 1.0;
      cell.isAnimated = YES;
   }];
}

Here Cell reference is Custom Cell reference. I hope this code works for you

Upvotes: 1

Nitesh Kumar Singh
Nitesh Kumar Singh

Reputation: 143

UITableView is a subclass of UIScrollView, and UITableViewDelegate conforms to UIScrollViewDelegate. So the delegate you attach to the table view will get events such as scrollViewDidScroll:, and you can call methods such as contentOffset on the table view to finding the scroll position.

Visit apple doc for more info on UIScrollViewDelegate

Use scrollViewDidScroll method and then put a condition there to find the scroll direction. And animate on scroll down.

May use the code as below:

(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    CGPoint offset = aScrollView.contentOffset;
    CGRect bounds = aScrollView.bounds;
    CGSize size = aScrollView.contentSize;
    UIEdgeInsets inset = aScrollView.contentInset;
    float y = offset.y + bounds.size.height - inset.bottom;
    float h = size.height;

    // NSLog(@"offset: %f", offset.y);   
    // NSLog(@"content.height: %f", size.height);   
    // NSLog(@"bounds.height: %f", bounds.size.height);   
    // NSLog(@"inset.top: %f", inset.top);   
    // NSLog(@"inset.bottom: %f", inset.bottom);   
    // NSLog(@"pos: %f of %f", y, h);

    float reload_distance = 10;
    if(y > h + reload_distance) {
       //write your code here to animate
           //cells.alpha = 0.0;
           //[UIView animateWithDuration:0.4 animations:^{
           //cells.alpha = 1.0;
           //}];
    }
}

Upvotes: 0

Related Questions