Stop a UITableView from automatically scrolling

2019-02-21 01:04发布

问题:

I have a UITableView that I'm autoscrolling with setContentOffset. Like so:

 CGFloat point = self.table.tblMinutes.contentSize.height - self.table.tblMinutes.bounds.size.height;

    [self.table.tblMinutes setContentOffset:CGPointMake(0, point) animated:false];
    [self.table.tblMinutes layoutIfNeeded];

    [UIView animateWithDuration:20.0 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        [self.table.tblMinutes setContentOffset:CGPointMake(0, point - 500) animated:false];
    } completion:nil];

What I want to achieve is for the scrolling to smoothly slow down and stop. I haven't been able to achieve that.

Calling [self.table.tblMinutes.layer removeAllAnimations] stops the animation, but moves the contentOffset for some reason, not achieving what I want.

I tried using the UIViewAnimationOptionBeginFromCurrentState option in a animation but that did nothing.

Any suggestions?

回答1:

This is a subset of the classic problem of interrupting an existing animation and replacing it with a different animation.

The problem here is that if you merely remove the existing animation, you will jump to the end of that animation.

The solution to that is to consult the presentation layer and set the layer to that position before removing the existing animation. Now you are starting from midstream and can apply another animation.



回答2:

Take a look at Kyle Truscott's excellent blog post about this for UIScrollView. UITableView inherits from UIScrollView, so in theory, it should work.