gpt4 book ai didi

ios - 如何通过在SwiftUI中触摸屏幕的任何位置来更改文本

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

大家好,我想通过触摸任意位置来随机更改文本,但是我只能通过触摸文本来更改文本。

var myArray = ["1", "2", "3"]

@State private var selectedSuggestion = Int.random(in: 0...2)

var body: some View {

Text(myArray[selectedSuggestion])
.frame(width: UIScreen.main.bounds.width * 1, height: UIScreen.main.bounds.height * 1)
.font(.largeTitle)
.multilineTextAlignment(.center)
.onTapGesture {
self.selectedSuggestion = Int.random(in: 0...2)

}
}

最佳答案

解决方法是添加内容形状

Text(myArray[selectedSuggestion])
.frame(width: UIScreen.main.bounds.width * 1, height: UIScreen.main.bounds.height * 1)
.font(.largeTitle)
.multilineTextAlignment(.center)
.contentShape(Rectangle()) // << here !!
.onTapGesture {
self.selectedSuggestion = Int.random(in: 0...2)

}

关于ios - 如何通过在SwiftUI中触摸屏幕的任何位置来更改文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62689609/

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