實例方法
coordinate
coordinateSpace(name:)
為視圖的坐標空間分配一個名稱,以便其他代碼可以對相對於命名空間的點和大小等維度進行操作。
聲明
func coordinateSpace<T>(name: T) -> some View where T : Hashable
參數
name
用於標識此坐標空間的名稱。
詳述
使用coordinateSpace(name:)
允許另一個函數查找和操作視圖,並操作相對於該視圖的維度。
下麵的示例演示了嵌套視圖如何查找和操作其封閉視圖的坐標空間:
struct ContentView: View {
@State var location = CGPoint.zero
var body: some View {
VStack {
Color.red.frame(width: 100, height: 100)
.overlay(circle)
Text("Location: \(Int(location.x)), \(Int(location.y))")
}
.coordinateSpace(name: "stack")
}
var circle: some View {
Circle()
.frame(width: 25, height: 25)
.gesture(drag)
.padding(5)
}
var drag: some Gesture {
DragGesture(coordinateSpace: .named("stack"))
.onChanged { info in location = info.location }
}
}
在這裏,名為“stack” 的ContentView
中的VStack
由一個紅框組成,其中心有一個自定義的Circle
視圖View/overlay(_:alignment:)
。
circle
視圖有一個附加的DragGesture
,它以封閉的 VStack 的坐標空間為目標。由於手勢識別器的關閉在 circle
中注冊事件,它將它們存儲在共享的 location
狀態變量中,並且 VStack
在 Text
視圖中顯示坐標。
可用版本
iOS 13.0+, iPadOS 13.0+, macOS 10.15+, Mac Catalyst 13.0+, tvOS 13.0+, watchOS 6.0+
相關用法
- Swift Optional colorMultiply(_:)用法及代碼示例
- Swift Optional confirmationDialog(_:isPresented:titleVisibility:presenting:actions:message:)用法及代碼示例
- Swift Optional confirmationDialog(_:isPresented:titleVisibility:presenting:actions:)用法及代碼示例
- Swift Optional contentShape(_:_:eoFill:)用法及代碼示例
- Swift Optional controlSize(_:)用法及代碼示例
- Swift Optional confirmationDialog(_:isPresented:titleVisibility:actions:)用法及代碼示例
- Swift Optional cornerRadius(_:antialiased:)用法及代碼示例
- Swift Optional contrast(_:)用法及代碼示例
- Swift Optional containerShape(_:)用法及代碼示例
- Swift Optional colorInvert()用法及代碼示例
- Swift Optional contextMenu(menuItems:)用法及代碼示例
- Swift Optional confirmationDialog(_:isPresented:titleVisibility:actions:message:)用法及代碼示例
- Swift Optional compositingGroup()用法及代碼示例
- Swift Optional clipShape(_:style:)用法及代碼示例
- Swift Optional clipped(antialiased:)用法及代碼示例
- Swift Optional symbolVariant(_:)用法及代碼示例
- Swift Optional popover(isPresented:attachmentAnchor:arrowEdge:content:)用法及代碼示例
- Swift Optional mask(alignment:_:)用法及代碼示例
- Swift Optional listSectionSeparatorTint(_:edges:)用法及代碼示例
- Swift Optional badge(_:)用法及代碼示例
- Swift Optional fullScreenCover(isPresented:onDismiss:content:)用法及代碼示例
- Swift Optional keyboardType(_:)用法及代碼示例
- Swift Optional preferredColorScheme(_:)用法及代碼示例
- Swift Optional background(_:ignoresSafeAreaEdges:)用法及代碼示例
- Swift Optional saturation(_:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 Optional coordinateSpace(name:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。