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

查看:13
本文介绍了交叉方向 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 来做到这一点(/one 是一个表格视图,但你明白了)?我已经用头撞墙了好几个小时了——如果你认为你能做到这一点,那么我会很乐意奖励赏金.


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.

子类 UITableView.UITableViewUIScrollView 的子类.在 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"类(具有多个 TableView)更改为您的 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天全站免登陆