UIScrollView在分页时确定scrollViewDidEndDragging的滚动方向

发布于 2024-12-11 01:56:03 字数 275 浏览 1 评论 0原文

有谁知道如何确定当用户抬起手指时滚动视图将向哪个方向移动,即当调用scrollViewDidEndDragging时?

具体来说,当滚动视图设置为分页时。

大多数时候,我可以跟踪并检查scrollViewDidScroll中的contentOffset,但是,也有一些特殊情况,即用户快速滑动屏幕。在某些情况下,左右移动将滚动到下一页,而在其他情况下,相同的图案将保留在同一页面上。

我猜这与触摸的加速度(最后几个点之间的差异)有关。

(我在iPad上使用iOS4.3)

Does anyone know how to determine which direction a scroll view will move when the user lifts their finger, i.e. when scrollViewDidEndDragging gets called?

Specifically, when the scroll view is set to paging.

Most of the time, I can just track and check the contentOffset in scrollViewDidScroll, however, there are special cases where the user flicks the screen quickly. In some cases a LEFT-RIGHT-LEFT move will scroll to the next page, and in others, the same pattern will remain on the same page.

I'm guessing it has something to do the with acceleration (difference between the last few points) of the touch.

(I'm on iOS4.3 on an iPad)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

雨巷深深 2024-12-18 01:56:03

您不使用计时器,这是非常昂贵的操作。首先锁定滚动视图的方向,使其仅向上/向下和向左/向右移动[我猜这就是你想要的]。然后使用以下代码来确定...

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);
}

You don't use timer, which is very expensive operation. Lock the direction of your scrollView first to move only up/down and left/right [I guess that's what you want]. And then use following code to determine...

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);
}
谈场末日恋爱 2024-12-18 01:56:03

这是我的解决方案,对我来说效果很好,希望它可以帮助

- (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;
}

我退出旧的解决方案,因为新的解决方案看起来更光滑

Here is my solution works fine for me hope it helps

- (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;
}

I quit the old solution as the new one seems more slick

浅紫色的梦幻 2024-12-18 01:56:03

实现以下 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");
    }

}

Implement the following UIScrollViewDelegate Method.
You can compare current content offset and targetContentOffset then get the direction.

-(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");
    }

}
预谋 2024-12-18 01:56:03

这是一个棘手的问题,iOS 5 有 回答,但这对你没有好处。

也许一个巧妙的解决方法是在 scrollViewDidEndDragging 完成后设置一个较小的时间间隔(例如 0.1 秒)的 NSTimer 。然后比较内容偏移量。

如果你想知道滚动视图是否真的会转到下一页,也许你可以检查内容偏移量是否超过了 1/2。您可以阅读 scrollViewDidScroll: 并进行一些数学计算以确定其是否超过 1/2 方式。

This is a tricky one, iOS 5 has the answer, but that no good for you.

Perhaps a hacky workaround is to set a NSTimer for a small time interval (say 0.1 seconds) after scrollViewDidEndDragging finished. Then compare the content offsets.

If you want to know if the scroll view will actually go to the next page, perhaps you could check if the content offset has gone more than 1/2 way. You could read the content offset on scrollViewDidScroll: and do a bit of maths to determine if its more than 1/2 way.

攀登最高峰 2024-12-18 01:56:03

另外还有索林反应。
她的解决方案工作正常,但如果您使用分页并且处于滚动内容大小的末尾,您将必须测试我们是否真的滚动到下一个“页面”:

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");
   }
}

In addition to saurin response.
Her solution work fine but if you are using pagination and you are in the end of scroll content size you will have to test if we really have a scroll to the next 'page' or not:

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");
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文