我在项目中使用 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 测试
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/