Greg
Greg

Reputation: 707

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.

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)
    }
}

Upvotes: 1

Views: 200

Answers (2)

Greg
Greg

Reputation: 707

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. All I needed to do was set its height to that of the UIView containing it.

The UITextView (displayData) is inside a UIView container. 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).

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.

enter image description here

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!

Upvotes: 0

Saurav_Sharma
Saurav_Sharma

Reputation: 140

Use the touchesEnded function, this function is called whenever a touch event is ended.

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
            // implement in this function, when fingers are lifted this function is called.
        }
        

Upvotes: 1

Related Questions