我有一张想要放入我的应用程序的地图,我目前正在学习 SwiftUI。我想放入一个 SVG,我可以在其中滚动、缩放、设置初始 x 和 y 坐标/初始缩放、最大缩放以及点击元素以打开页面。我知道我可以用 UIKit 做到这一点,但是如何用 SwiftUI 做到这一点?如果我无法使用 SVG 执行此操作,如何使用交互式光栅图像执行此操作?
编辑:这是我的代码:
struct ContentView: View {
@State private var currentPosition: CGSize = .zero
@State private var newPosition: CGSize = .zero
@State private var scale: CGFloat = 1.0
@State private var currentAmount: CGFloat = 0
@State private var finalAmount: CGFloat = 1
var body: some View {
Image("SVG")
.frame(width: 1100, height: 1100)
.offset(x: self.currentPosition.width, y: self.currentPosition.height)
.gesture(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)
print(self.newPosition.width)
print(self.newPosition.height)
self.newPosition = self.currentPosition
}
)
.scaleEffect(finalAmount + currentAmount)
.gesture(
MagnificationGesture()
.onChanged { amount in
self.currentAmount = amount - 1
}
.onEnded { amount in
self.finalAmount += self.currentAmount
self.currentAmount = 0
}
)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}