![](/img/trans.png)
[英]Scroll to selected position in scrollview when view loads - SwiftUI
[英]How to check view scrollable position when scrollview scroll
我們已使用文本視圖實施了粘性流出廣告功能。 當文本視圖滾動時,我們在文本視圖文本的中間添加了一個視圖。 當textview滾動和添加的視圖從視圖中出來時。 然后我們顯示另一個視圖,其中添加了相同的視圖內容。 它完美無缺。 但現在我的新要求是用戶滾動文本視圖並顯示添加的視圖。 然后我們需要根據視圖外觀顯示第二個添加的視圖。 像添加的視圖只有50%出現在屏幕上然后我們需要顯示第二個視圖。 現在我的問題是如何根據滾動位置檢測視圖外觀。 用於隱藏或顯示視圖。 提前致謝
您必須使用scrollViewDidScroll
委托方法。 您可以嘗試以下方法:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
// Table View Scroll Image
let viewContentOffset = textview.contentOffset
let viewContentSize = textview.contentSize
let viewBounds = textview.bounds.size
// Use Alpha value to manipulation
let bottomOffset = scrollView.contentSize.height - scrollView.bounds.size.height
let alpha = (scrollView.contentOffset.y / bottomOffset) * 2
}
使用NSAttributtedString將圖像添加到TextView:
textView.text = "Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda."
let image = UIImage(named: "sampleImage.png");
let attachment = NSTextAttachment()
attachment.image = image
let attString = NSAttributedString(attachment: attachment)
textView.textStorage.insert(attString, at: textView.text.count/2)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.