简体   繁体   English

滚动视图滚动时如何检查视图可滚动位置

[英]How to check view scrollable position when scrollview scroll

We have implemented sticky out stream Ad functionality by using text view. 我们已使用文本视图实施了粘性流出广告功能。 When text view scroll we have show added one view in mid of text view text. 当文本视图滚动时,我们在文本视图文本的中间添加了一个视图。 And when textview scroll and added view is going out from view. 当textview滚动和添加的视图从视图中出来时。 then we have show another view with same content of added view. 然后我们显示另一个视图,其中添加了相同的视图内容。 It works perfect. 它完美无缺。 But now my new requirement is when user scroll the text view and added view appears. 但现在我的新要求是用户滚动文本视图并显示添加的视图。 Then we need to show the second added view according to view appearance. 然后我们需要根据视图外观显示第二个添加的视图。 like added view is only 50% appear on screen then we need to show the second view. 像添加的视图只有50%出现在屏幕上然后我们需要显示第二个视图。 Now my problem is how to detect the view appearance according to scroll position. 现在我的问题是如何根据滚动位置检测视图外观。 for hide or show view. 用于隐藏或显示视图。 Thanks in Advance 提前致谢

You have to use scrollViewDidScroll delegate method for that. 您必须使用scrollViewDidScroll委托方法。 You can try with following: 您可以尝试以下方法:

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
}

Add Image to TextView using NSAttributtedString: 使用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.

相关问题 在视图加载时滚动到滚动视图中选定的 position - SwiftUI - Scroll to selected position in scrollview when view loads - SwiftUI 出现键盘时如何使滚动视图在缩小的视图区域中滚动 - How to make the scrollview scrollable in reduced view area when keyboard appears 当我在TabBarController中切换视图时,ScrollView停止滚动,如何解决? - When i switch View In TabBarController the ScrollView Stop Scroll,How to Solve it? 自定义scrollView滚动时,移动标题视图 - Custom scrollView when Scroll, move header view 如何使用滚动视图制作可滚动标签? - how to make scrollable label using scroll view? 如何仅禁用 ScrollView 中的滚动而不禁用内容视图? - How to only disable scroll in ScrollView but not content view? 如何与作为滚动视图子视图的水平滚动视图进行交互? - How to interact with a horizontal scrollview that is a subview of a scroll view? 如何在特定位置将视图添加到ScrollView? - How to add a View to a ScrollView at a specific position? 当我垂直向下滚动时,scrollView Resets是位置 - The scrollView Resets is position when i scroll down vertically 如何在可滚动视图上修复AVPlayer位置或如何在视图中修复它 - How to fix AVPlayer position on scrollable view or How to fix it in a view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM