首页 > 解决方案 > 如何确定在 SwiftUI 中缩放和平移图像的位置?

问题描述

使用 SwiftUI 进行开发时,我使用的是 Image(),并且希望不仅能够放大它,我目前正在这样做,而且还希望放大它的不同区域并平移到不同的区域. 看来我只能从一个区域放大它作为缩放的原点(中心),但想从图像上手势开始的位置缩放。这是到目前为止的代码:

ScrollView([.horizontal, .vertical]) {
                Image(uiImage: UIImage(data: self.document.image)!)
                .resizable()
                .scaledToFit()
                .padding()
                .cornerRadius(8)
                .scaleEffect(self.scale, anchor: .center)
                .frame(width: geo.size.width, height: geo.size.height, alignment: .center)
                .gesture(MagnificationGesture()
                    .onChanged { val in
                        let delta = (val / self.lastScale)
                        self.lastScale = val
                        self.scale = (self.scale * delta)
                    }
                    .onEnded { val in
                        self.lastScale = 1.0
                    }
                )
                .onTapGesture(count: 2) {
                    self.scale = 1.0
                    self.lastScale = 1.0
                }
            }
            .frame(width: geo.size.width - 50, height: geo.size.height - 90, alignment: .center)
            .clipped()

对此的任何帮助将不胜感激。

更新的代码似乎对我有用:

Image(uiImage: UIImage(data: self.document.image)!)
                .resizable()
                .scaledToFill()
                .padding()
                .cornerRadius(10)
                .scaleEffect(self.finalAmount + self.currentAmount)
                .offset(x: self.currentPosition.width, y: self.currentPosition.height)
                .frame(width: geo.size.width, height: geo.size.height, alignment: .center)
                .clipped()
                .scaleEffect(1.15)
                .sheet(isPresented: self.$isSheetActive) {
                    ShareSheet(activityItems: [self.MakeImageFileToShare(uiimage: UIImage(data: self.document.image)!)])
                }
                .gesture(MagnificationGesture()
                    .onChanged { value in
                         if self.finalAmount + value - 1 >= 1 {
                                  self.currentAmount = value - 1
                              }
                          }
                          .onEnded { amount in
                              self.finalAmount += self.currentAmount
                          self.currentAmount = 0
                    }
                .simultaneously(with: DragGesture()
                    .onChanged({ value in
                        self.currentPosition = CGSize(width: value.translation.width + self.newPosition.width, height: value.translation.height + self.newPosition.height)
                    })
                    .onEnded ({ value in
                        self.currentPosition = CGSize(width: value.translation.width + self.newPosition.width, height: value.translation.height + self.newPosition.height)
                        self.newPosition = self.currentPosition
                })))

标签: swiftuigesture

解决方案


推荐阅读