【问题标题】:Adding gestures to uiimageview in uiscrollview (pan, doubletap, pinch)在 uiscrollview 中向 uiimageview 添加手势(平移、双击、捏合)
【发布时间】:2017-06-10 16:24:17
【问题描述】:

目前我正在尝试创建某种图像查看器,您可以在其中单击图像,然后以全尺寸显示。现在我想为其添加手势以进行缩放。我想了解并了解如何添加捏合手势来放大和缩小,以便能够平移图像并使用双击手势快速放大。我没有找到很多好的教程。

我知道您放大的是视图,而不是图像。这就是您使用包含 ImageView 的 ScrollView 的原因。现在缺少什么来启用缩放、捏合和移动图像?

提前感谢您提供任何有用的帖子。

以下是此功能的“我的”当前代码库。需要补充什么?

class DetailViewController: UIViewController, UIScrollViewDelegate {

var scrollView: UIScrollView!
var imageView: UIImageView!

override func viewDidLoad()
{
    super.viewDidLoad()

    self.navigationController?.isNavigationBarHidden = false

    scrollView=UIScrollView()
    scrollView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: (self.view.frame.height - (self.navigationController?.navigationBar.frame.size.height)! + 44))
    scrollView.minimumZoomScale=1
    scrollView.maximumZoomScale=3
    scrollView.bounces=false
    scrollView.delegate=self
    self.view.addSubview(scrollView)

    imageView=UIImageView()
    imageView.image = UIImage(named: "inlinelogo.jpg")
    imageView.frame = CGRect(x: 0, y: 0, width: scrollView.frame.width, height: scrollView.frame.height - (44 + UIApplication.shared.statusBarFrame.size.height))
    imageView.backgroundColor = .black
    imageView.contentMode = .scaleAspectFit
    scrollView.addSubview(imageView)

}

func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView?
    {
    return imageView
    }
}

【问题讨论】:

  • 启用 imageView userInteractionEnabled 属性

标签: ios uiscrollview uiimageview zooming uigesturerecognizer


【解决方案1】:

你错过了UIImageView添加的userInteractionEnabled属性

imageView.isuserInteractionEnabled = true

【讨论】:

    猜你喜欢
    • 2021-11-30
    • 2012-08-13
    • 2019-06-20
    • 1970-01-01
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多