實例方法
on
onTapGesture(count:perform:)
添加當此視圖識別輕擊手勢時要執行的操作。
聲明
func onTapGesture(
count: Int = 1,
perform action: @escaping () -> Void
) -> some View
參數
count
觸發
action
中提供的操作關閉所需的點擊或點擊次數。默認為1
。action
要執行的操作。
詳述
當用戶單擊或點擊視圖或容器count
次時,使用此方法執行指定的action
。
在下麵的示例中,每當用戶單擊或輕擊視圖兩次時,心髒圖像的顏色都會從 colors
數組中更改為隨機顏色:
struct TapGestureExample: View {
let colors: [Color] = [.gray, .red, .orange, .yellow,
.green, .blue, .purple, .pink]
@State private var fgColor: Color = .gray
var body: some View {
Image(systemName: "heart.fill")
.resizable()
.frame(width: 200, height: 200)
.foregroundColor(fgColor)
.onTapGesture(count: 2) {
fgColor = colors.randomElement()!
}
}
}
可用版本
iOS 13.0+, iPadOS 13.0+, macOS 10.15+, Mac Catalyst 13.0+, tvOS 16.0+ Beta, watchOS 6.0+
相關用法
- Swift Never onSubmit(of:_:)用法及代碼示例
- Swift Never onChange(of:perform:)用法及代碼示例
- Swift Never opacity(_:)用法及代碼示例
- Swift Never overlay(_:in:fillStyle:)用法及代碼示例
- Swift Never offset(x:y:)用法及代碼示例
- Swift Never overlay(_:ignoresSafeAreaEdges:)用法及代碼示例
- Swift Never overlay(alignment:content:)用法及代碼示例
- Swift Never offset(_:)用法及代碼示例
- Swift Never pageCommand(value:in:step:)用法及代碼示例
- Swift Never colorMultiply(_:)用法及代碼示例
- Swift Never accessibilityAction(action:label:)用法及代碼示例
- Swift Never alert(isPresented:error:actions:message:)用法及代碼示例
- Swift Never confirmationDialog(_:isPresented:titleVisibility:actions:)用法及代碼示例
- Swift Never flipsForRightToLeftLayoutDirection(_:)用法及代碼示例
- Swift Never position(x:y:)用法及代碼示例
- Swift Never position(_:)用法及代碼示例
- Swift Never luminanceToAlpha()用法及代碼示例
- Swift Never accessibilityAction(_:_:)用法及代碼示例
- Swift Never previewDisplayName(_:)用法及代碼示例
- Swift Never badge(_:)用法及代碼示例
- Swift Never progressViewStyle(_:)用法及代碼示例
- Swift Never buttonStyle(_:)用法及代碼示例
- Swift Never popover(isPresented:attachmentAnchor:arrowEdge:content:)用法及代碼示例
- Swift Never projectionEffect(_:)用法及代碼示例
- Swift Never background(_:ignoresSafeAreaEdges:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 Never onTapGesture(count:perform:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。