onReceive函数导致自定义textview中的URL闪烁?

问题描述

我设置了一个计时器,每几秒钟调用一次:

let timer = Timer.publish(every: 5,on: .main,in: .common).autoconnect()

我有一个.onReceive()函数,每当计时器达到5秒标记时都会调用函数

    var body: some View {
    ZStack {
        MapView().onReceive(self.timer) { input in
            self.database.fetchItems()
            dispatchQueue.main.async {
                for annotation in self.annotations.annos {
                    if annotation !== self.selectedAnnotation {
                        self.map.removeAnnotation(annotation)
                    }
                }
                for item in self.database.items {
                    
                    self.annotations.addNextAnnotation(ID: item.id)
                    
                    var int = 0
                    for annotation in self.annotations.annos {
                        if annotation.title! == "\(item.id)" {
                            self.annotationsVM.annos.remove(at: int)
                            break
                        }
                        int = int + 1
                    }
                }
            }
        }
        vstack {
            TextView(text: NSAttributedString(string: "testing this thing https://google.com and putting some text after"),mapStyle: "light")
        }
    }
}
} 

我实现了一个自定义文本视图,并在文本本身中检测到的链接添加了下划线:

class MyTextView: UITextView,UITextViewDelegate {
func textView(_ textView: UITextView,shouldInteractWith URL: URL,in characterRange: NSRange,interaction: UITextItemInteraction) -> Bool {
    print(URL)
    UIApplication.shared.open(URL)
    return false
}
}

struct TextView: UIViewRepresentable {
var text: NSAttributedString
var mapStyle: String

func makeUIView(context: Context) -> MyTextView {
    let view = MyTextView()
    view.dataDetectorTypes = .link
    view.isEditable        = false
    view.isSelectable      = true
    view.delegate          = view
    view.backgroundColor = UIColor.clear
    view.isUserInteractionEnabled = false
    if self.mapStyle == "dark" {
        view.linkTextAttributes = [NSAttributedString.Key.underlinestyle: NSUnderlinestyle.single.rawValue,NSAttributedString.Key.foregroundColor: UIColor.white]
    } else {
        view.linkTextAttributes = [NSAttributedString.Key.underlinestyle: NSUnderlinestyle.single.rawValue,NSAttributedString.Key.foregroundColor: UIColor.black]
    }
    return view
}

func updateUIView(_ uiView: MyTextView,context: Context) {
    uiView.attributedText = text
    if self.mapStyle == "dark" {
        uiView.textColor = UIColor.white
    } else {
        uiView.textColor = UIColor.black
    }
    uiView.font = UIFont(name: "HelveticaNeue",size: 16)
}
}

我遇到的问题是在渲染的文本视图中,它可以正确检测链接并使它可单击以及所有这些好东西,但是由于某种原因,每当调用onReceive函数时,可单击的链接就会闪烁。有什么想法吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)