every day i’m learning
I made this blog post almost exactly one year ago. At the time, I thought it was kind of a big deal. Today, I discovered that it was not that big of a deal at all.
I was working on a project that required that code. After I implemented it, I noticed that the the scrolling was kind of jerky and not nearly as smooth as it should be. It was very subtle, but it bothered me enough to do some Googling.
That’s when I found this post on stackoverflow. I was skeptical this would solve my problem, but I gave it a try anyway. It worked perfectly, and it made my other solution seem ham-fisted.
So I ended up going from this:
- (void) animateScroll:(NSTimer *)timerParam { const NSTimeInterval duration = 10.2; NSTimeInterval timeRunning = -[startTime timeIntervalSinceNow]; if (timeRunning >= duration) { [scrollView setContentOffset:destinationOffset animated:YES]; [timer invalidate]; timer = nil; return; } CGPoint offset = [scrollView contentOffset]; offset.y = startOffset.y + (destinationOffset.y - startOffset.y) * timeRunning / duration; [scrollView setContentOffset:offset animated:YES]; } - (void) doAnimatedScrollTo:(CGPoint)offset { self.startTime = [NSDate date]; startOffset = scrollView.contentOffset; destinationOffset = offset; if (!timer) { self.timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(animateScroll:) userInfo:nil repeats:YES]; } } |
To this:
- (void) doAnimatedScrollTo:(CGPoint)offset { [UIScrollView beginAnimations:@"scrollAnimation" context:nil]; [UIScrollView setAnimationDuration:1.5]; [scrollView setContentOffset:offset]; [UIScrollView commitAnimations]; } |
So much slicker and much more elegant. I lol’ed at myself.
This post is part of iDevBlogADay, a group of blogs by indie iPhone developers featuring new posts every day. You can subscribe to iDevBlogADay through RSS or follow the #iDevBlogADay hash tag or @idevblogaday on Twitter.
March 6th, 2012 at 6:40 pm
The content size is the total size of what you are iwniveg. For example my content was 294 wide, and my scroll view size was 73.5. When looking in the debugger, some of the scrollview properties were rounded up, and others weren’t, hence the view thought it wasn’t at a page position when rounding had occurred.When I changed the content size to 292 and the view size to 73, the problem went away as there was no rounding to be done.
March 26th, 2012 at 5:06 am
[...] [a Weblog by Aaron Griffith] « every day i’m learning [...]