[英]How to stop UITextView from scrolling when fingers are lifted (Swift 4)
I have a textView whose content may exceed the height of the view, so I would like to be able to scroll down, stopping when I want, and to scroll back up when returning after having stopped.我有一个 textView 其内容可能会超过视图的高度,所以我希望能够向下滚动,在需要时停止,并在停止后返回时向上滚动。
Problems :问题:
When scrolling (with two fingers), two things happen:滚动(用两根手指)时,会发生两件事:
Questions :问题:
import UIKit
class ScrollableView: UIView, UITextViewDelegate {
var displayData = UITextView()
override func draw(_ rect: CGRect) {
super.draw(rect)
displayData.delegate = self
getContent()
calculateFrameBounds()
displayTallTable()
}
func getContent() {
}
func calculateFrameBounds() {
}
func displayTallTable() {
displayData.frame = CGRect(x: x, y: y, width: w, height: h)
self.addSubview(displayData)
}
}
Use the touchesEnded function, this function is called whenever a touch event is ended.使用 touchesEnded 函数,每当触摸事件结束时都会调用此函数。
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
// implement in this function, when fingers are lifted this function is called.
}
Partial answer:部分答案:
To be able to stop and resume scrolling, it turns out that the height of UITextView ( displayData
) needs to be smaller than its contents.为了能够停止和恢复滚动,事实证明 UITextView (
displayData
) 的高度需要小于其内容。 All I needed to do was set its height to that of the UIView containing it.我需要做的就是将其高度设置为包含它的 UIView 的高度。
The UITextView ( displayData
) is inside a UIView container. UITextView (
displayData
) 位于 UIView 容器内。 To prevent displayData
's text from scrolling into the adjacent views (above and below the UIView container) I only had to turn on clipsToBounds on the UIView container like this: super.clipsToBounds = true
(or check the box in IB).为了防止
displayData
的文本滚动到相邻的视图(UIView 容器的上方和下方),我只需要像这样打开 UIView 容器上的 clipsToBounds: super.clipsToBounds = true
(或选中 IB 中的框)。
This is only a partial solution, however.然而,这只是部分解决方案。 Because I have gaps between the UITextView (
displayData
) and the top and bottom of its UIView container, the data still scrolls off the UITextView and into the gaps of the UIView.因为我在 UITextView (
displayData
) 和它的 UIView 容器的顶部和底部之间有间隙,所以数据仍然从 UITextView 滚动到 UIView 的间隙中。
I have tried all of the following without success:我已经尝试了以下所有方法都没有成功:
displayData.clipsToBounds = true
displayData.contentInset = UIEdgeInsets.zero
displayData.layer.masksToBounds = true
Any help with this last piece would be greatly appreciated!对最后一块的任何帮助将不胜感激!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.