交叉方向UIScrollViews-我可以修改滚动行为吗? [英] Cross Directional UIScrollViews - Can I Modify the Scrolling Behaviour?

查看:61
本文介绍了交叉方向UIScrollViews-我可以修改滚动行为吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是滚动视图的工作方式::一个滚动视图已在水平方向上启用了分页.此滚动视图的每个页面"都包含一个垂直滚动的UITableView.如果不进行修改,则可以正常运行,但效果并不理想.

Here's how the scroll views work: One scroll view is paging enabled in the horizontal direction. Each 'page' of this scroll view contains a vertically scrolling UITableView. Without modification, this works OK, but not perfectly.

不正确的行为:当用户在表格视图上向上滚动和向下滚动,但又想快速翻动到下一页时,最初的水平翻动/滑动将不起作用-在表格视图静止之前,该功能将不起作用(即使滑动非常清晰地处于水平状态).

The behaviour that's not right: When the user scrolls up and down on the table view, but then wants to flick over to the next page quickly, the horizontal flick/swipe will not work initially - it will not work until the table view is stationary (even if the swipe is very clearly horizontal).

工作原理:如果滑动明显是水平的,即使表格视图仍在滚动/弹跳,我也希望页面发生变化,因为这也是用户的期望.

How it should work: If the swipe is clearly horizontal, I'd like the page to change even if the table view is still scrolling/bouncing, as this is what the user will expect too.


如何更改这种行为-最简单或最佳的方法是什么?


注意由于种种原因,某些答案中所述的UIPageViewController将不起作用.如何使用横向UIScrollViews(/一个是表格视图,但您明白了)吗?我一直把头撞在墙上好几个小时了-如果您认为可以做到的话,那么我将乐意为您提供赏金.


NOTE For various reasons, a UIPageViewController as stated in some answers will not work. How can I do this with cross directional UIScrollViews (/one is a table view, but you get the idea)? I've been banging my head against a wall for hours - if you think you can do this then I'll more than happily award a bounty.

推荐答案

根据我对问题的理解,只有在tableView滚动时,我们才想更改默认行为.其他所有行为都相同.

According to my understanding of the question, it is only while the tableView is scrolling we want to change the default behaviour. All the other behaviour will be the same.

子类. UITableViewUIScrollView s的子类.在UITableView子类上,实现一个UIScrollViewUIGestureRecognizer的委托方法

SubClass UITableView. UITableViews are subClass of UIScrollViews. On the UITableView subClass implement one UIScrollView's UIGestureRecognizer's delegate method

- (BOOL)gestureRecognizer:(UIPanGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UISwipeGestureRecognizer *)otherGestureRecognizer
{
    //Edit 1
    //return self.isDecelerating;
    //return self.isDecelerating | self.bounces; //If we want to simultaneous gesture on bounce and scrolling
    //Edit 2
    return self.isDecelerating || self.contentOffset.y < 0 || self.contentOffset.y > MAX(0, self.contentSize.height - self.bounds.size.height); // @Jordan edited - we don't need to always enable simultaneous gesture for bounce enabled tableViews
}

因为我们只想在tableView减速时更改默认手势行为.

As we only want to change the default gesture behaviour while the tableView is decelerating.

现在将所有'UITableView'的类更改为新创建的tableViewSubClass并运行该项目,在tableView滚动时滑动即可工作. :]

Now change all 'UITableView's class to your newly created tableViewSubClass and run the project, swipe should work while tableView is scrolling. :]

但是,在tableView滚动时,滑动看起来有些敏感.让我们对滑动进行一些限制.

But the swipe looks a little too sensitive while tableView is scrolling. Let's make the swipe a little restrictive.

子类UIScrollView.在UIScrollView子类上,实现另一个UIGestureRecognizer的委托方法gestureRecognizerShouldBegin:

SubClass UIScrollView. On the UIScrollView subclass implement another UIGestureRecognizer's delegate method gestureRecognizerShouldBegin:

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer 
{
    if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
        CGPoint velocity = [(UIPanGestureRecognizer *)gestureRecognizer velocityInView:self];
        if (abs(velocity.y) * 2 < abs(velocity.x)) {
            return YES;
        }
    }
    return NO;
}

我们要使滑动明显是水平的".以上代码仅在x轴上的手势速度是y轴上的手势速度的两倍时才允许手势开始. [如果愿意,可以随意增加硬编码值"2".滑动值越高,需要水平的程度就越高.]

We want to make the "swipe is clearly horizontal". Above code only permits gesture begin if the gesture velocity on x axis is double than on y axis. [Feel free to increase the hard coded value "2" if your like. The higher the value the swipe needs to be more horizontal.]

现在将"UiScrollView"类(具有多个TableViews)更改为ScrollViewSubClass.运行项目. :]

Now change the `UiScrollView' class (which has multiple TableViews) to your ScrollViewSubClass. Run the project. :]

我已经在gitHub上创建了一个项目 https://github.com/rishi420/SwipeWhileScroll

I've made a project on gitHub https://github.com/rishi420/SwipeWhileScroll

这篇关于交叉方向UIScrollViews-我可以修改滚动行为吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆