我正在尝试将我的手势提取到一个函数中,以便在我的一个 Swift 包中使用。我遇到的问题是,当我尝试在我的一个 View 中使用它时,它不再符合 View 。
以下代码会产生此错误: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
}
})
}
}
最佳答案
改用some
,some
指示编译器从内部生成和返回的任何内容推断具体类型:
func swipeDownGesture() -> some Gesture { // << here !!
DragGesture(minimumDistance: 0, coordinateSpace: .local).onEnded({ gesture in
if gesture.translation.height > 0 {
// Run some code
}
})
}
关于ios - 通过函数使用Gesture时类型 'any View'不能符合 'View',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73083463/