简体   繁体   English

抬起手指时如何阻止 UITextView 滚动(Swift 4)

[英]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:滚动(用两根手指)时,会发生两件事:

  • the top lines scroll into the view above顶行滚动到上面的视图中
  • when fingers are lifted from the screen, the view just bounces back to the top.当手指从屏幕上抬起时,视图会弹回顶部。

Questions :问题

  • How do I prevent the data from scrolling above the textView frame into the view above it?如何防止数据在 textView 框架上方滚动到其上方的视图中?
  • How do I stop the scrolling when they take their fingers off the screen?当他们的手指离开屏幕时如何停止滚动? What event(s) get called when the user lifts their fingers?当用户抬起手指时会调用什么事件?
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.

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