简体   繁体   English

如何做UITextView滚动检测和更改图像

[英]How to do UITextView scroll detect and change images

It looks like this : 它看起来像这样: 在此输入图像描述

在此输入图像描述

(I can't show image please I don't have 10 prestiges) (我无法显示图像请我没有10个声望)

let departmentMessage = UITextView(frame: CGRect(x: 25, y: 1500 /      2, width: UIScreen.main.bounds.size.width - 50, height: (UIScreen.main.bounds.size.height - 50)/4 ))
    showBlshText()
    departmentMessage.isScrollEnabled = true
    departmentMessage.isPagingEnabled = true
    departmentMessage.isEditable = false
    departmentMessage.isSelectable = true
    departmentMessage.dataDetectorTypes = UIDataDetectorTypes.link 
    departmentMessage.textColor = UIColor.darkGray

    departmentMessage.textAlignment = .left
    departmentMessage.contentMode = .topLeft

    departmentMessage.backgroundColor = UIColor(displayP3Red: 100, green: 100, blue: 100, alpha: 20)
    departmentMessage.font = UIFont(name: "Helvetica-Light", size: 20)
    self.view.addSubview(departmentMessage)
    departmentMessage.removeFromSuperview()

I expect the output of the word to change, but the actual output isn't work. 我希望单词的输出改变,但实际输出不起作用。

UITextView is a subclass of UIScrollView . UITextViewUIScrollView的子类。 So everything that a scrollView does a textView can do also. 因此,scrollView执行textView的所有功能也可以。 In you case, it seems that you want to do some action when the textView is scrolled. 在你的情况下,似乎你想在滚动textView时做一些动作。 You should look the UIScrollViewDelegate documentation which describes how you can monitor a scrollView to see when it is scrolled. 您应该查看UIScrollViewDelegate文档,该文档描述了如何监视scrollView以查看它何时滚动。 There are a few tips to know: 有一些提示要知道:

  • the delegate is not called when you set the content offset programatically. 以编程方式设置内容偏移量时,不会调用委托。
  • scrollViewDidEndDecelerating: is not called when scrollViewDidEndDragging:willDecelerate: is called with NO for the willDecelerate value. scrollViewDidEndDecelerating:scrollViewDidEndDragging:willDecelerate:没有为willDecelerate值调用时调用。

So just set your viewController as the delegate, implement the appropriate methods, and change your UI accordingly. 因此,只需将viewController设置为委托,实现适当的方法,并相应地更改UI。

Also, you might have better luck using a UICollectionView or a UITableView. 此外,使用UICollectionView或UITableView可能会更好。 Even if every row is just a label, it is far easier to track what indexPath is at the top of the collectionView then what text is at the top of textView. 即使每一行只是一个标签,也可以更容易地跟踪collectionView顶部的indexPath,然后是textView顶部的文本。 But without knowing exactly what you are doing it is hard to say. 但是,如果不知道你在做什么,很难说。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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