問題描述
我需要做這個配置奇怪的應用程序.
I need to do this app that has a weird configuration.
如下圖所示,主視圖是一個 UIScrollView.那么里面應該有一個UIPageView,而PageView的每一頁都應該有一個UITableView.
As shown in the next image, the main view is a UIScrollView. Then inside it should have a UIPageView, and each page of the PageView should have a UITableView.
到目前為止,我已經完成了這一切.但我的問題是我希望滾動表現 自然.
I've done all this so far. But my problem is that I want the scrolling to behave naturally.
接下來是我的意思自然.目前,當我在其中一個 UITableViews 上滾動時,它會滾動表格視圖(而不是滾動視圖).但我希望它滾動 ScrollView,除非滾動視圖無法滾動,因為它到達了頂部或底部(在這種情況下,我希望它滾動 tableview).
The next is what I mean naturally. Currently when I scroll on one of the UITableViews, it scrolls the tableview (not the scrollview). But I want it to scroll the ScrollView unless the scrollview cannot scroll cause it got to its top or bottom (In that case I'd like it to scroll the tableview).
例如,假設我的滾動視圖當前滾動到頂部.然后我將手指放在(正在顯示的當前頁面的)tableview 上并開始滾動向下.在這種情況下,我希望滾動視圖滾動(沒有表格視圖).如果我繼續向下滾動滾動視圖并到達底部,如果我將手指從顯示屏上移開并將其放回 tebleview 上并再次向下滾動,我希望我的 tableview 現在向下滾動,因為滾動視圖到達了底部并且不是可以繼續滾動.
For example, let's say my scrollview is currently scrolled to the top. Then I put my finger over the tableview (of the current page being shown) and start scrolling down. I this case, I want the scrollview to scroll (no the tableview). If I keep scrolling down my scrollview and it reaches the bottom, if I remove my finger from the display and put it back over the tebleview and scroll down again, I want my tableview to scroll down now because the scrollview reached its bottom and it's not able to keep scrolling.
你們知道如何實現這種滾動嗎?
Do you guys have any idea about how to implement this scrolling?
我真的很迷茫.任何幫助將不勝感激:(
I'm REALLY lost with this. Any help will be greatly appreciate it :(
謝謝!
推薦答案
同時處理滾動視圖和表格視圖的解決方案圍繞UIScrollViewDelegate
展開.因此,讓您的視圖控制器符合該協議:
The solution to simultaneously handling the scroll view and the table view revolves around the UIScrollViewDelegate
. Therefore, have your view controller conform to that protocol:
class ViewController: UIViewController, UIScrollViewDelegate {
我將滾動視圖和表格視圖表示為插座:
I’ll represent the scroll view and table view as outlets:
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var tableView: UITableView!
我們還需要跟蹤滾動視圖內容的高度以及屏幕高度.稍后你會明白為什么.
We’ll also need to track the height of the scroll view content as well as the screen height. You’ll see why later.
let screenHeight = UIScreen.mainScreen().bounds.height
let scrollViewContentHeight = 1200 as CGFloat
viewDidLoad:
:
override func viewDidLoad() {
super.viewDidLoad()
scrollView.contentSize = CGSizeMake(scrollViewContentWidth, scrollViewContentHeight)
scrollView.delegate = self
tableView.delegate = self
scrollView.bounces = false
tableView.bounces = false
tableView.scrollEnabled = false
}
我已關閉彈跳以保持簡單.關鍵設置是滾動視圖和表格視圖的委托,并且首先關閉表格視圖滾動.
where I’ve turned off bouncing to keep things simple. The key settings are the delegates for the scroll view and the table view and having the table view scrolling being turned off at first.
這些是必要的,以便 scrollViewDidScroll:
委托方法可以處理到達滾動視圖的底部和到達表格視圖的頂部.這是那個方法:
These are necessary so that the scrollViewDidScroll:
delegate method can handle reaching the bottom of the scroll view and reaching the top of the table view. Here is that method:
func scrollViewDidScroll(scrollView: UIScrollView) {
let yOffset = scrollView.contentOffset.y
if scrollView == self.scrollView {
if yOffset >= scrollViewContentHeight - screenHeight {
scrollView.scrollEnabled = false
tableView.scrollEnabled = true
}
}
if scrollView == self.tableView {
if yOffset <= 0 {
self.scrollView.scrollEnabled = true
self.tableView.scrollEnabled = false
}
}
}
委托方法所做的是檢測滾動視圖何時到達底部.當這種情況發生時,表格視圖可以滾動.它還會檢測表格視圖何時到達重新啟用滾動視圖的頂部.
What the delegate method is doing is detecting when the scroll view has reached its bottom. When that has happened the table view can be scrolled. It is also detecting when the table view reaches the top where the scroll view is re-enabled.
我創建了一個 GIF 來展示結果:
I created a GIF to demonstrate the results:
這篇關于如何使 ScrollView 中的 TableView 的滾動行為自然的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!