[英]Setting UIScrollView content offset in a UITableViewCell doesn't work in non-reused dequeued cells
[英]content offset doesn't work
我正在嘗試在我的 uiscrollview 中設置內容偏移量。 我已經嘗試了以下方法,但都沒有奏效。 以下都不會以任何方式影響我的滾動視圖,它只是正常顯示在屏幕上。:
let point = CGPoint(x: 0, y: self.view.frame.height / 2)
self.scrollView.setContentOffset(point, animated: true)
和:
self.scrollView.contentOffset.y = self.view.frame.size.height / 2
和:
self.scrollView.contentOffset = CGPointMake(0, self.view.frame.size.height / 2)
和:
self.scrollView.contentOffset = CGPointMake(0, scrollView.frame.size.height / 2)
我的滾動視圖是視圖高度的 2 倍。 它們基本上分為 2 個不同的視圖,我可以在它們之間進行分頁。 我希望滾動視圖從底部開始。
您應該在viewDidLayoutSubviews
嘗試
當我發現將 scrollView.contentOffset 設置為任何值都不起作用時,我真的很撓頭。 最終我意識到我正在調用 scrollView 委托消息“scrollViewDidScroll”來管理滾動拖動特性。
事實證明,每當更改 scrollView contentOffset 時都會調用“scrollViewDidScroll”,而我管理拖動問題的代碼正在更改 contentOffset。 我剛剛制作了一個“BOOL isDragging”變量,以便在拖動時只更改 contentOffset:
private var isDragging : Bool = false
private var initialContentOffset = CGPoint.zero
public func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
isDragging = true
...
}
public func scrollViewDidScroll(_ scrollView: UIScrollView) {
// need to keep track if we are dragging with isDragging
// since this 'scrollViewDidScroll' API is called with ANY contentOffset change
if (isDragging == true){
// do stuff to contentOffset to manage dragging peculiarities
...
}
}
public func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if (decelerate == false){
scrollViewDidEndScrollingAnimation(scrollView)
}
}
public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
scrollViewDidEndScrollingAnimation(scrollView)
}
public func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
isDragging = false
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.