Created
July 25, 2014 15:39
-
-
Save Air-Craft/41f13e03766e4824d4b7 to your computer and use it in GitHub Desktop.
ScrollView willScrollToEnd simulation #ios #scrolling #physics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView | |
{ | |
const CGFloat DECEL = 500; // pixels/second decrease in velocity per second. need to guess this | |
CGPoint vel = [self.panGestureRecognizer velocityInView:self.superview]; | |
if (vel.y > 0) return; | |
CGFloat bottomEdge = scrollView.contentOffset.y + scrollView.frame.size.height; | |
CGFloat distanceToBottom = scrollView.contentSize.height -bottomEdge; | |
// v1 = v + at | |
// 0 = v + at | |
// t = -v / a | |
// d = d0 + vt + 0.5 * at^2 | |
// = -v^2 / a + 0.5 * v^2 / a | |
CGFloat a = -DECEL; | |
CGFloat v = -vel.y; | |
CGFloat decelDist = -v*v/a + 0.5*v*v/a; | |
if (distanceToBottom < decelDist) { | |
DLOG(@"WILL scroll to end!") | |
} else { | |
DLOG(@"WONT scroll to end!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment