gpt4 book ai didi

ios - 在 SwiftUI 的 UITextView 中禁用向上滚动

转载 作者:行者123 更新时间:2023-12-01 19:30:19 25 4
gpt4 key购买 nike

我在项目中使用 UITextView,我需要它只能向下滚动(不能向上滚动)。有没有办法可以相对容易地做到这一点?如果不是,我可能会继承 UIScrollView 或一些奇怪的东西,但我真的不想这样做。
这是我的基本 UIViewRepresentable 结构

struct TextView: UIViewRepresentable {

typealias UIViewType = UITextView
var configuration = { (view: UIViewType) in }

func makeUIView(context: UIViewRepresentableContext<Self>) -> UIViewType {
let view = UIViewType()
view.isEditable = false

return view
}

func updateUIView(_ uiView: UIViewType, context: UIViewRepresentableContext<Self>) {
configuration(uiView)
}
}
谢谢!

最佳答案

可以通过 UITextView 的简单子(monad)类来完成处理平移手势激活。
使用 Xcode 12 测试
demo

class BottomScrollTextView: UITextView {
override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
guard let gr = gestureRecognizer as? UIPanGestureRecognizer else { return true }

return gr.translation(in: self).y < 0 // << allow only to down
}

private var _contentOffset = CGPoint.zero
override var contentOffset: CGPoint {
get { super.contentOffset }
set {
if newValue.y > super.contentOffset.y || self.isDecelerating {
super.contentOffset = newValue
}
}
}
}

struct TextView: UIViewRepresentable {

typealias UIViewType = UITextView
var configuration = { (view: UIViewType) in }

func makeUIView(context: UIViewRepresentableContext<Self>) -> UIViewType {
let view = BottomScrollTextView() // << here !!

// ... other code

关于ios - 在 SwiftUI 的 UITextView 中禁用向上滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63198668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com