InteractiveImageView 是一个开源库,它提供了像 iPhone 照片 App 那样的图片显示体验。(点击缩放,捏合和拖动手势,保持位置的旋转,运动惯性)。该视图支持 UIKit 和 SwiftUI 框架。
@State var tapLocation: CGPoint = .zero
InteractiveImage(image: .init(named: "Iceland"), zoomInteraction: .init(location: tapLocation, scale: 1.2, animated: true)
iOS 16 之前的 SwiftUI 不支持触摸位置检测。为了缩放到触摸位置,你可以使用 Gestures 包。
.onTouchGesture(count: 2) { gesture in
tapLocation = gesture.location
}
var interactiveImageView = InteractiveImageView(maxScale: 2.0)
interactiveImageView.image = UIImage(named: "Iceland")
func gestureHandler(_ sender: UITapGestureRecognizer) {
let location = sender.location(in: imageView)
interactiveImageView.zoom(to: location, scale: 2.0, animated: true)
}
将以下依赖项添加到你的 Package.swift 文件中
.package(url: "https://github.com/vospennikov/InteractiveImageView.git", .upToNextMinor(from: "1.1.0"))
Gestures 根据 MIT 许可证提供。有关更多信息,请参阅 LICENSE 文件。