SwiftUI ScrollView 如何在带有长文本的 tvOS 上滚动

问题描述 投票:0回答:2

我需要渲染一段很长的文字,如何用ScrollView包裹起来然后垂直滑动? (tvOS)

我试了下面的代码,在

.focusable()
中加上
Text
可以专注,但是滑动不流畅,会一直走到最后

struct ContentView: View {
    
    let longlongText = Array(repeating: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.", count: 20).joined()
    
    var body: some View {
        ScrollView {
            VStack {
                Button("Hello, world!") {}
                
                Text(longlongText)
                    .focusable()
            }
            .padding()
        }
    }
}

swiftui scrollview tvos
2个回答
0
投票

我是一个超级初学者,我遇到了同样的问题。感谢 https://qiita.com/abouch/items/8fa4c8e7777fd4ed37db,我设法实现了这一点,尽管我仍然不明白 View 和 UIView 是如何协同工作的。

struct TextView: UIViewRepresentable {
  @Binding var attributedText: NSAttributedString
  @Binding var offset: Int

  func makeCoordinator() -> Coordinator {
    Coordinator(self)
  }
  
  func makeUIView(context: Context) -> UITextView {
    let textView = UITextView()
    textView.delegate = context.coordinator
    textView.isScrollEnabled = true
    textView.isUserInteractionEnabled = true
    textView.backgroundColor = .clear
    return textView
  }
  
  func updateUIView(_ textView: UITextView, context: Context) {
    textView.attributedText = attributedText
    textView.setContentOffset(CGPoint(x: 0, y: offset*10), animated: true)
  }
}

extension TextView {
  final class Coordinator: NSObject, UITextViewDelegate {
    private var textView: TextView
    
    init(_ textView: TextView) {
      self.textView = textView
    }
    
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
      return true
    }
    
    func textViewDidChange(_ textView: UITextView) {
      self.textView.attributedText = textView.attributedText
    }
  }
}

在SwiftUI中初始化是这样的:

TextView(attributedText:$longText, offset:$offset)

在视图上添加向上和向下按钮,然后由按钮动作触发的递增/递减偏移量进行滚动。


0
投票

对我来说,以下代码在 tvos 中有效

    [_textView setScrollEnabled:YES];
    [_textView setUserInteractionEnabled:YES];
    [_textView setSelectable:YES];
    _textView.panGestureRecognizer.allowedTouchTypes = [NSArray arrayWithObjects:[NSNumber numberWithInteger:UITouchTypeIndirect], nil];

快速使用

    textView.isUserInteractionEnabled = true;
    textView.isScrollEnabled = true;
    textView.selectable = true;
    textView.panGestureRecognizer.allowedTouchTypes = [NSNumber(value: UITouch.TouchType.indirect.rawValue)]```
© www.soinside.com 2019 - 2024. All rights reserved.