問題描述
滾動視圖的工作原理如下: 一個滾動視圖在水平方向啟用分頁.此滾動視圖的每個頁面"都包含一個垂直滾動的 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
.UITableView
是 UIScrollView
的子類.在 UITableView
子類上實現一個 UIScrollView
的 UIGestureRecognizer
的委托方法
SubClass UITableView
. UITableView
s are subClass of UIScrollView
s. 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 - 我可以修改滾動行為嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!