如何检测何时敲击TextField? (在MacOS上为SwiftUI)

时间:2019-11-22 04:40:54

标签: textfield swiftui gesture tap

如何检测何时点击TextField? (在MacOS上为SwiftUI)

import SwiftUI

struct ContentView: View {
  @State var field: String = "TextField"

  var body: some View {
    TextField("Fill in Text", text: $field)
      .onTapGesture {
        print("Textfield pressed")
    }
  }
}

在点按TextField时,它不会打印“ Textfield按下”。 我猜这是因为在Textfield手势可以检测到水龙头之前,TextField拾取了水龙头。

任何帮助表示赞赏<|:-)

2 个答案:

答案 0 :(得分:0)

.simultaneousGesture修饰符可以满足您的需求。

这里是一个例子:

struct TestTextTap: View {
    @State var field: String = "TextField"

    var body: some View {
      TextField("Fill in Text", text: $field)
        .simultaneousGesture(TapGesture().onEnded {
          print("Textfield pressed")
        })
    }
}

答案 1 :(得分:0)

您可以检测Form_Resize何时轻松聚焦在private void Form1_Resize(object sender, EventArgs e) { Size maxChartSize = Size - _outerMargin; Size innerSize = maxChartSize - _innerMargin; double currentAspectRatio = (float)innerSize.Width / innerSize.Height; if (currentAspectRatio < _aspectRatio) { int chartWidth = Width - _outerMargin.Width; chart1.Width = chartWidth; chart1.Height = (int)((chartWidth - _innerMargin.Width) / _aspectRatio + _innerMargin.Height); } else { int chartHeight = Height - _outerMargin.Height; chart1.Height = chartHeight; chart1.Width = (int)((chartHeight - _innerMargin.Height) * _aspectRatio + _innerMargin.Width); } } 内,而不是进行录音:

TextField