实例方法
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:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。