Pregunta

I've implemented some code, which is working nicely, to scroll a UIScrollview automatically based on a timer being triggered.

Here it is:

....
CGPoint offset;
....
offset = scroller.contentOffset;
....
- (void) scrollWords: (NSTimer *) theTimer
{
offset.y = offset.y+300;
[UIScrollView beginAnimations:@"scrollAnimation" context:nil];
[UIScrollView setAnimationDuration:50.0f];
[scroller  setContentOffset:offset];
[UIScrollView commitAnimations];

}

However, I've noticed that the while the scrolling is happening, the scroll rate varies; half way through it scrolls 2 or 3 lines of text per second, but at the start and finish it's much slower, perhaps only 0.5 lines per second. Is there any way of controlling the scroll rate?

Thanks in advance.

Paul.

¿Fue útil?

Solución

You're looking for setAnimationCurve:. Specifically what you're describing is the effect of UIViewAnimationCurveEaseInOut. Try adding [UIScrollView setAnimationCurve:UIAnimationCurveLinear];

Also, you're using old style animation code. If you're targeting iOS 4 or above, check out this new style that's much more friendly (in my opinion):

- (void) scrollWords: (NSTimer *) theTimer
{
    offset.y = offset.y+300;
    [UIScrollView animateWithDuration:50.0f delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        [scroller setContentOffset:offset];
    }];
}

Using the delay parameter, you can probably even get rid of your NSTimer. With this code you can scroll the table view after 5 seconds.

- (void) scrollWordsLater
{
    offset.y = offset.y+300;
    [UIScrollView animateWithDuration:50.0f delay:5.0 options:UIViewAnimationOptionCurveLinear animations:^{
        [scroller setContentOffset:offset];
    }];
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top