I am trying to reproduce the smooth animation of a scrollview with paging enabled when you actually scroll to the next page. It seems to be UIViewAnimationCurveEaseInOut
, but I need to have a "next page" button and trig the scroll programmatically.
Here is my code :
-(void) scrollToPage:(int)page
{
UIScrollView *scrollView = contentView;
CGPoint offset = CGPointMake(scrollView.bounds.size.width * page, scrollView.contentOffset.y);
[scrollView setContentOffset:offset animated: YES];
[self pageControlUpdate];
}
-(void) scrollToNextPage
{
[self scrollToPage:(pageControl.currentPage + 1)];
}
I cannot manage to reproduce the smoothness of UIViewAnimationCurveEaseInOut
,
either with setContentOffset
, or with scrollRectToVisible
...
it goes to the next page with an ugly linear animation
I even tried to animate it manually :
[UIView animateWithDuration:.5 delay:0 options:UIViewAnimationCurveEaseInOut animations:^{
scrollView.contentOffset = offset;
} completion:^(BOOL finished) { } ];
where am I wrong ?
With the use of public APIs, I don't believe this is currently possible. Assuming you don't need user interaction during the course of the animation, you'd be better off animating the position of your
UIScrollView
's subviews (ie. the scroll view's content) instead, and then adjusting thecontentOffset
without animation on completion. You could do it like so:I can confirm this works as expected, I tested it myself.
Every time you make your own animations you have to pass NO as
animated:
parameter:This class absolutely saved my life:
MOScroll on GitHub.com
It has
- (void)setContentOffset:(CGPoint)contentOffset withTimingFunction:(CAMediaTimingFunction *)timingFunction duration:(CFTimeInterval)duration;
Just like the private API but with all public methods and math.