A gesture to attach to the view.
gesture(_:including:)
Availability 有効性
- iOS 13.0+
- iPadOS 13.0+
- macOS 10.15+
- Mac Catalyst 13.0+
- tvOS 13.0+
- watchOS 6.0+
Technology
- Swift
UI
Declaration 宣言
func gesture<T>(_ gesture: T, including mask: GestureMask
= .all) -> some View
where T : Gesture
Parameters パラメータ
gesture
mask
A value that controls how adding this gesture to the view affects other gestures recognized by the view and its subviews. Defaults to
all
.
Discussion 議論
Use this method when you need to attach a gesture to a view. The example below defines a custom gesture that prints a message to the console and attaches it to the view’s VStack
. Inside the VStack
a red heart Image
defines its own Tap
handler that also prints a message to the console, and blue rectangle with no custom gesture handlers. Tapping or clicking the image prints a message to the console from the tap gesture handler on the image, while tapping or clicking the rectangle inside the VStack
prints a message in the console from the enclosing vertical stack gesture handler.
struct GestureExample: View {
private var message = "Message"
let newGesture = TapGesture().onEnded {
print("Tap on VStack.")
}
var body: some View {
VStack(spacing:25) {
Image(systemName: "heart.fill")
.resizable()
.frame(width: 75, height: 75)
.padding()
.foregroundColor(.red)
.onTapGesture {
print("Tap on image.")
}
Rectangle()
.fill(Color.blue)
}
.gesture(newGesture)
.frame(width: 200, height: 200)
.border(Color.purple)
}
}