【发布时间】:2019-05-27 12:56:26
【问题描述】:
我正在尝试弄清楚如何向 imageView 添加捏合/缩放功能, 但特别是我以编程方式添加到 scrollView 的那个。我能够在 SO 上找到一些很好的示例,以使用带有 viewForZooming 的滚动视图来捏合和缩放图像,但在那种情况下,我必须返回被捏合和缩放的图像视图,如果我以编程方式返回它,这将不起作用。
我的最终目标是拥有一组图像,用户可以在其中左右滚动以查看所有图像并能够放大它们,基本上就像他们在翻阅照片流一样。我找到了一个不错的教程,用于动态添加图像以在此处滚动https://www.codementor.io/taiwoadedotun/ios-swift-implementing-photos-app-image-scrolling-with-scroll-views-bkbcmrgz5#comments-bkbcmrgz5,但我不清楚如何添加 viewForZooming,因为图像视图正在循环中动态 .addSubview。
我创建了一个小示例,其中包含与帖子关联的 0-n 图像的集合视图。一旦点击带有图像的 collectionViewCell,就会出现一个隐藏的滚动视图,并添加一个新的动态 UIImageView 作为子视图。一切都很好,但我现在不知道如何添加捏/缩放。
@objc func imageTapped(_ sender: UITapGestureRecognizer) {
print("BlipeFile Image Tapped")
let imageView = sender.view as! UIImageView
let newImageView = UIImageView(image: imageView.image)
//newImageView.frame = UIScreen.main.bounds
newImageView.contentMode = .scaleAspectFit
newImageView.clipsToBounds = true
newImageView.layer.borderColor = UIColor.gray.cgColor
newImageView.layer.borderWidth = 3.0
newImageView.frame = self.view.frame
newImageView.backgroundColor = .black
newImageView.isUserInteractionEnabled = true
newImageView.image = imageView.image
let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage))
newImageView.addGestureRecognizer(tap)
scroller.isHidden = false
scroller.addSubview(newImageView)
}
@objc func dismissFullscreenImage(_ sender: UITapGestureRecognizer) {
scroller.isHidden = true
self.navigationController?.isNavigationBarHidden = false
self.tabBarController?.tabBar.isHidden = false
sender.view?.removeFromSuperview()
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return image //Can't return dynamically created newImageView?
}
【问题讨论】:
标签: ios swift uiscrollview uiimageview