简体   繁体   中英

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. 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. 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. 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:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM