0

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?

irosenb
  • 828
  • 2
  • 13
  • 26
  • 1
    Why aren't you calling `setContentOffset:animated:`? – matt Nov 17 '15 at 21:14
  • @matt I'm calling it within a `[UIView animateWithDuration]` block, with the `UIViewAnimationOptionBeginFromCurrentState` option. Nothing happens – irosenb Nov 17 '15 at 21:18
  • Sorry, I'm just confused about what you're trying to achieve. Smoothly slow down and stop, that sounds like a custom animation curve, so why isn't that all you need? In other words I don't see the point of your two-stage linear plus completion. Just animate to where you want to animate to...? – matt Nov 17 '15 at 21:20
  • @matt I removed the linear + completion. It's not important. Basically the slowdown occurs after the user taps a "notification". – irosenb Nov 17 '15 at 21:30
  • 1
    Ah. So you mean, you are _in the middle_ of an animation and you want to _change_ it suddenly while it's still going on? – matt Nov 17 '15 at 21:31
  • @matt Exactly. Nothing I've tried has worked well :( – irosenb Nov 17 '15 at 21:32

2 Answers2

2

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Ahhhh interesting. I'll give this a shot – irosenb Nov 17 '15 at 21:39
  • 1
    For an example, look here: https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch04p133cancelAnimation/ch17p484cancelAnimation/ViewController.swift Work your way through the examples; I think case 3 is closest to what you are after, and you should be able to use it as a basis. – matt Nov 17 '15 at 21:39
  • Works like a charm . Thanks so much – irosenb Nov 17 '15 at 21:46
0

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

Roy Falk
  • 1,685
  • 3
  • 19
  • 45
  • No, I don't see how that's relevant. That's for _manual_ scrolling, and it's what table view's already do. – matt Nov 17 '15 at 21:24