简体   繁体   English

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

[英]How Can I Change The Text By Touching Anywhere Of The Screen In SwiftUI

Hello everyone I want to change the text randomly by touching anywhere but I can just change it by touching the text.大家好,我想通过触摸任意位置来随机更改文本,但我可以通过触摸文本来更改它。

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)
            
    }
}

The solution is to add content shape解决方案是添加内容形状

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)

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM