UIScrollView direction from scrollViewWillBeginDecelerating
Is it possible to retrieve the direction that a UIScrollView
is scrolling to, in the method;
-(void) scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
If so, how can this be achieved?
you could try something like this:
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
xCoord = scrollview.contentOffset.x;
}
-(void) scrollViewWillBeginDecelerating:(UIScrollView *)sender
{
if (xCoord > scrollView.contentOffset.x) {
//RIGHT
} else if (xCoord < scrollView.contentOffset.x) {
//LEFT
}
}
store the x coord of the scrollViews contentOffset when it begins scrolling, then compare it in scrollViewWillBeginDecelerating
In the case of paging try this
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
_xCoord = scrollView.contentOffset.x;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (_xCoord > scrollView.contentOffset.x) {
//right
} else if (_xCoord < scrollView.contentOffset.x) {
//LEFT
}
}
Not directly answering the question, but might be useful for some – to retrieve the direction that a UIScrollView
will decelerate:
func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
let point = scrollView.panGestureRecognizer.translation(in: scrollView)
if point.x < 0 {
// Will decelerate to the right
}else if point.x > 0{
// Will decelerate to the left
}
}
精彩评论