gpt4 book ai didi

ios - SwiftUI:无法向下拖动来关闭 UIImagePickerController

转载 作者:行者123 更新时间:2023-12-01 22:34:27 35 4
gpt4 key购买 nike

我使用以下代码来显示默认的 UIImagePickerController,但似乎 iOS 13 的向下拖动关闭手势不是'当使用包装在 SwiftUI View 中的 UIImagePickerController 时,用户仍然可以点击默认的取消选项卡栏项目来关闭选择器,代码:

struct ContentView: View {
@State private var showModal = false
var body: some View {
Text("Tap To show modal")
.padding()
.onTapGesture {
self.showModal.toggle()
}
.sheet(isPresented: $showModal) {
SomeUIKitView()
}
}
}

final class SomeUIKitViewControllerWrapper: UIViewControllerRepresentable {
typealias UIViewControllerType = UIImagePickerController

func makeUIViewController(context: Context) -> UIImagePickerController {
let imagePicker = UIImagePickerController()
imagePicker.sourceType = UIImagePickerController.SourceType.photoLibrary
return imagePicker
}
func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {}
}

struct SomeUIKitView: View {
var body: some View {
SomeUIKitViewControllerWrapper()
}
}

enter image description here

最佳答案

您可以在 SwiftUI 中构建自己的拖动。在您的情况下,您可以尝试为您的 SomeUIKitView() View 实现它。

import SwiftUI

struct SwiftUIDragDown: View {
@State private var dragDownTreshhold: CGFloat = 50
@State private var draggedDistance: CGFloat = 0
@State private var dragDownOffsetY: CGFloat = 0


var body: some View {
Text("Drag me")
.offset(x: 0, y: dragDownOffsetY + draggedDistance)
.animation(.easeInOut(duration: 0.3))
.gesture(
DragGesture(minimumDistance: 10)
.onChanged { value in
print("dragged by" , value.translation.height)
self.draggedDistance = value.translation.height
}
.onEnded { _ in
if self.draggedDistance > self.dragDownTreshhold {
self.dragDownOffsetY = 600
} else {
self.draggedDistance = 0
self.dragDownOffsetY = 0
}
}
)
}
}

struct SwiftUIDragDown_Previews: PreviewProvider {
static var previews: some View {
SwiftUIDragDown()
}
}

关于ios - SwiftUI:无法向下拖动来关闭 UIImagePickerController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60485214/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com