0

我正在尝试解决拖动手势的一个小问题:我尝试了在 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)
    }
}

任何帮助表示赞赏!谢谢!

4

0 回答 0