UIScrollView determines the scroll direction on scrollViewDidEndDragging when swapping

Does anyone know how to determine in which direction the scroll will move when the user lifts his finger, i.e. when is scrollViewDidEndDragging called?

In particular, when paging is set to represent the scroll.

In most cases, I can simply track and check the contentOffset in scrollViewDidScroll, however there are special cases when the user quickly switches the screen. In some cases, the LEFT-RIGHT-LEFT movement will scroll to the next page, while in others the template will remain on one page.

I assume that he needs to do something with the acceleration (the difference between the last few points) of the touch.

(I'm on iOS4.3 on iPad)

+5
source share
5 answers

It is difficult, iOS 5 has the answer , but it is not suitable for you.

Perhaps a hacky workaround is to install NSTimerin a short period of time (say, 0.1 seconds) after completion scrollViewDidEndDragging. Then compare the offset content.

If you want to know if the scroll view really goes to the next page, you can probably check if the content offset went more than 1/2 way. You can read the content bias scrollViewDidScroll:and do some math to determine if it has more than 1/2 way.

+1
source

, . scrollView, / / [ , ]. ...

CGPoint start, end;

- (void)scrollViewWillBeginDragging:(UIScrollView *)sender {
    start = self.scrollView.contentOffset;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)sender willDecelerate:(BOOL)decelerate {
    end = self.scrollView.contentOffset;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)sender {    
    NSLog(@"%d %d",(int)start.x,(int)start.y);
    NSLog(@"%d %d",(int)end.x,(int)end.y);
}
+3

, ,

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

   CGFloat pageWidth = scrollView.frame.size.width;

   //Old Solution
   //Switch the indicator when more than 30% of the previous/next page is visible
   //You can vary the percentage
   //int page = floor((scrollView.contentOffset.x - pageWidth * 0.3) / pageWidth) + 1;


   //New Solution
    _pageControl.currentPage = (int)scrollView.contentOffset.x / (int)pageWidth;
}

,

+2

UIScrollViewDelegate. targetContentOffset, .

-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{

    CGPoint newOffset = CGPointMake(targetContentOffset->x, targetContentOffset->y);

    if (newOffset.x > scrollView.contentOffset.x) {
        NSLog(@"Scrolling direction is left");
    }else{
        NSLog(@"Scrolling direction is right");
    }

}
+2

. , , , "" :

CGPoint start, end;

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    start = scrollView.contentOffset;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    end = scrollView.contentOffset;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
   if (start.x > end.x && end.x>0)
   {
       NSLog(@"on the previous left page");
   }

   if (start.x < end.x && end.x < scrollView.contentOffset.x)
   {
       NSLog(@"on the next right page");
   }
}
0

All Articles