How to get notified when scrollToRowAtIndexPath finishes animating

前端 未结 6 1869
梦如初夏
梦如初夏 2020-11-30 09:57

This is a follow-up to How to get notified when a tableViewController finishes animating the push onto a nav stack.

In a tableView I want to deselect a

6条回答
  •  时光说笑
    2020-11-30 10:18

    To address Ben Packard's comment on the accepted answer, you can do this. Test if the tableView can scroll to the new position. If not, execute your method immediately. If it can scroll, wait until the scrolling is finished to execute your method.

    - (void)someMethod
    {
        CGFloat originalOffset = self.tableView.contentOffset.y;
        [self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
        CGFloat offset = self.tableView.contentOffset.y;
    
        if (originalOffset == offset)
        {
            // scroll animation not required because it's already scrolled exactly there
            [self doThingAfterAnimation];
        }
        else
        {
            // We know it will scroll to a new position
            // Return to originalOffset. animated:NO is important
            [self.tableView setContentOffset:CGPointMake(0, originalOffset) animated:NO];
            // Do the scroll with animation so `scrollViewDidEndScrollingAnimation:` will execute
            [self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
        }
    }
    
    - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
    {
        [self doThingAfterAnimation];
    }
    

提交回复
热议问题