Как ограничить жест перетаскивания в SwiftUI

Я пытаюсь решить небольшую проблему с помощью жеста перетаскивания: я пробовал разные решения, которые нашел в Stack Overflow, но ни одно из них не работает для меня ... Когда я увеличиваю изображение и перетаскиваю его, если я слишком сильно перетаскиваю изображение, просто исчезнуть с экрана; То же самое, если я уменьшаю масштаб, если я уменьшаю масштаб слишком быстро, изображение выходит за пределы экрана ... Еще одна небольшая странность, которая у меня есть, заключается в том, что когда я уменьшаю масштаб, изображение не переходит автоматически в центр экрана ...

Вот код на данный момент:

      struct GalleryDetail: View {
    
    var busso: BussoModel
    
    private let minZoom: CGFloat = 1.0
    private let maxZoom: CGFloat = 5.0
    
    @GestureState private var magnificationLevel: CGFloat = 1
    @State private var zoomLevel: CGFloat = 1
    
    @State private var currentPosition: CGSize = .zero
    @State private var newPosition: CGSize = .zero
    
    @Environment(\.colorScheme) var colorScheme
    
    var body: some View {
        ZStack {
            URLImage(URL(string: busso.fotoUrl) ?? furl)
                .resizable()
                .aspectRatio(contentMode: .fit)
                .clipped()
                .scaleEffect(setZoom(magnification: magnificationLevel))
                .offset(x: self.currentPosition.width, y: self.currentPosition.height)
                .scaledToFit()
                .gesture(MagnificationGesture().updating($magnificationLevel, body: { (value, state, _) in
                    state = value
                }).onEnded({ (value) in
                    self.zoomLevel = self.setZoom(magnification: value)
                }).simultaneously(with:DragGesture(minimumDistance: 0, coordinateSpace: .global)
                                    .onChanged({ value in
                                        if zoomLevel == 1 {
                                            currentPosition = CGSize.zero
                                            newPosition = CGSize.zero
                                        } else {
                                            self.currentPosition = CGSize(width: value.translation.width + self.newPosition.width, height: value.translation.height + self.newPosition.height)
                                        }
                                    }).onEnded({ value in
                                        if zoomLevel == 1 {
                                            currentPosition = CGSize.zero
                                            newPosition = CGSize.zero
                                        } else {
                                            self.currentPosition = CGSize(width: value.translation.width + self.newPosition.width, height: value.translation.height + self.newPosition.height)
                                            print(self.newPosition.width)
                                            self.newPosition = self.currentPosition
                                        }
                                    })))
                .animation(.linear)
            VStack {
                Spacer()
                if colorScheme == .light {
                    Text(busso.testo)
                        .padding(.horizontal)
                        .padding(.bottom)
                        .shadow(color: .white, radius: 2)
                        .shadow(color: .white, radius: 2)
                        .shadow(color: .white, radius: 2)
                        .shadow(color: .white, radius: 2)
                    Text(busso.extra2)
                        .padding(.horizontal)
                        .padding(.bottom)
                        .shadow(color: .white, radius: 2)
                        .shadow(color: .white, radius: 2)
                        .shadow(color: .white, radius: 2)
                        .shadow(color: .white, radius: 2)
                } else {
                    Text(busso.testo)
                        .padding(.horizontal)
                        .padding(.bottom)
                        .shadow(color: .black, radius: 2)
                        .shadow(color: .black, radius: 2)
                        .shadow(color: .black, radius: 2)
                        .shadow(color: .black, radius: 2)
                    Text(busso.extra2)
                        .padding(.horizontal)
                        .padding(.bottom)
                        .shadow(color: .black, radius: 2)
                        .shadow(color: .black, radius: 2)
                        .shadow(color: .black, radius: 2)
                        .shadow(color: .black, radius: 2)
                }
            }
        }
        .navigationTitle(busso.titolo)
        .navigationBarTitleDisplayMode(.large)
    }
    
    func setZoom(magnification: CGFloat) -> CGFloat {
        return max(min(self.zoomLevel * magnification, self.maxZoom),self.minZoom)
    }
}

Любая помощь приветствуется! Спасибо!

0 ответов

Другие вопросы по тегам