简体   繁体   English

抬起手指时如何停止 UIScrollView (Swift 4)

[英]How to stop UIScrollView when fingers are lifted (Swift 4)

How do I stop a scrollView when the user lifts their fingers instead of just bouncing back to the top?当用户抬起手指而不是弹回顶部时,如何停止滚动视图?

var displayData = UITextView()

func displayTallTable() {

    displayData.text = outputString
    
    // getFrameBounds (x, y, width, height)
    displayData.frame = CGRect(x: x, y: y, width: w, height: h)
    
    self.addSubview(scrollView)
    scrollView.addSubview(displayData)

    let g = self.safeAreaLayoutGuide
    
    scrollView.leadingAnchor.constraint(equalTo: g.leadingAnchor).isActive = true
    scrollView.trailingAnchor.constraint(equalTo: g.trailingAnchor).isActive = true
    scrollView.topAnchor.constraint(equalTo: g.topAnchor).isActive = true
    scrollView.bottomAnchor.constraint(equalTo: g.bottomAnchor).isActive = true        
}

I can drag the view up but when I lift my fingers, it just bounces back to the top.我可以向上拖动视图,但是当我抬起手指时,它只会弹回顶部。

So, I added UIScrollViewDelegate to the class:所以,我将UIScrollViewDelegate添加到类中:
class TallView: UIView, UIScrollViewDelegate { ... }

and have tried the following, none of which is called:并尝试了以下方法,但均未调用:

func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
    debugPrint("scrollViewWillBeginDecelerating")
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
    debugPrint("scrollViewDidScroll")
}
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
    debugPrint("scrollViewWillBeginDragging")
}
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
    debugPrint("scrollViewWillEndDragging")
}
func scrollViewDidChangeAdjustedContentInset(_ scrollView: UIScrollView) {
    debugPrint("scrollViewDidChangeAdjustedContentInset")
}

It seems that :看起来 :

func killScroll() {
    self.scrollView.isScrollEnabled = false;
    self.scrollView.isScrollEnabled = true;
}

(from: How can I programmatically force-stop scrolling in a UIScrollView? ) should do the trick, but I don't know what should be calling it. (来自: 如何以编程方式在 UIScrollView 中强制停止滚动? )应该可以解决问题,但我不知道应该怎么称呼它。

What am I missing?我错过了什么?

If I understand you correctly, you may want this this effect:如果我理解正确,您可能想要这种效果:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if !scrollView.isTracking {
            let contentOffset = scrollView.contentOffset
            // if you want to locate to the top, you could set: let contentOffset = CGPoint.zero
            scrollView.setContentOffset(contentOffset, animated: false)
        }
    }
}

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

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