Код: Выделить всё
didUpdateContentsOfКод: Выделить всё
import SwiftUI
import QuickLook
struct ContentView: View {
@State private var currentIndex = 0
@State private var imageURLs: [URL] = []
var body: some View {
ZStack {
if !imageURLs.isEmpty {
// QuickLook View
QuickLookView(urls: imageURLs, currentIndex: $currentIndex)
.allowsHitTesting(false) // Disable QuickLook's own gestures
}
}
.highPriorityGesture(
DragGesture(minimumDistance: 30)
.onChanged { value in
print("Drag detected: \(value.translation.width)")
}
.onEnded { value in
let horizontalMovement = value.translation.width
let swipeThreshold: CGFloat = 50
if abs(horizontalMovement) > swipeThreshold {
if horizontalMovement > 0 {
// Swipe right - previous
if currentIndex > 0 {
currentIndex -= 1
print("Previous: \(currentIndex)")
}
} else {
// Swipe left - next
if currentIndex < imageURLs.count - 1 {
currentIndex += 1
print("Next: \(currentIndex)")
}
}
}
}
)
.onAppear {
loadSampleImages()
}
}
private func loadSampleImages() {
// Load sample images from bundle
if let url1 = Bundle.main.url(forResource: "image1", withExtension: "jpg"),
let url2 = Bundle.main.url(forResource: "image2", withExtension: "jpg") {
imageURLs = [url1, url2]
}
}
}
struct QuickLookView: UIViewControllerRepresentable {
let urls: [URL]
@Binding var currentIndex: Int
func makeUIViewController(context: Context) -> QLPreviewController {
let controller = QLPreviewController()
controller.dataSource = context.coordinator
controller.delegate = context.coordinator
controller.currentPreviewItemIndex = currentIndex
controller.view.backgroundColor = .clear
// Disable user interaction on QuickLook
controller.view.isUserInteractionEnabled = false
return controller
}
func updateUIViewController(_ uiViewController: QLPreviewController, context:
Context) {
if uiViewController.currentPreviewItemIndex != currentIndex {
uiViewController.currentPreviewItemIndex = currentIndex
}
// Ensure interaction remains disabled
uiViewController.view.isUserInteractionEnabled = false
}
func makeCoordinator() -> Coordinator {
Coordinator(parent: self)
}
class Coordinator: NSObject, QLPreviewControllerDataSource,
QLPreviewControllerDelegate {
let parent: QuickLookView
init(parent: QuickLookView) {
self.parent = parent
}
func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
return parent.urls.count
}
func previewController(_ controller: QLPreviewController, previewItemAt
index: Int) -> QLPreviewItem {
return parent.urls[index] as NSURL
}
}
}
[*] Использование. Allowshittesting (false) int QuickLook
Настройка контроллера. /> Добавление прозрачных представлений о наложении < /li>
< /ul>
Ожидаемое поведение: перевод влево /справа должно запустить жест перетаскивания и изменить
currentindex для перемещения между изображениями.>
Подробнее здесь: https://stackoverflow.com/questions/797 ... n-visionos
Мобильная версия