我正在尝试将手势提取到一个函数中,以便在我的 Swift 包之一中使用。我遇到的问题是,当我尝试在我的视图之一上使用它时,它不再符合视图。
以下代码会产生此错误:Type 'any View' cannot conform to 'View'
struct ContentView: View {
var body: some View {
VStack {
Text("Placeholder")
}
.gesture(swipeDownGesture())
}
func swipeDownGesture() -> any Gesture {
DragGesture(minimumDistance: 0, coordinateSpace: .local).onEnded({ gesture in
if gesture.translation.height > 0 {
// Run some code
}
})
}
}
Run Code Online (Sandbox Code Playgroud)
usesome相反,some指示编译器从内部生成和返回的内容推断具体类型:
func swipeDownGesture() -> some Gesture { // << here !!
DragGesture(minimumDistance: 0, coordinateSpace: .local).onEnded({ gesture in
if gesture.translation.height > 0 {
// Run some code
}
})
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5841 次 |
| 最近记录: |