Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/118.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 无法缩放图像视图_Ios_Swift_Scrollview - Fatal编程技术网

Ios 无法缩放图像视图

Ios 无法缩放图像视图,ios,swift,scrollview,Ios,Swift,Scrollview,我不熟悉斯威夫特 我正在做一个应用程序开发手册中的练习。我正在尝试创建一个UIImageView并在其中放置图像,但无法放大图像。我的代码有问题,但我不知道发生了什么 import UIKit class ViewController: UIViewController, UIScrollViewDelegate { @IBOutlet weak var image: UIImageView! @IBOutlet weak var scroll: UIScrollView!

我不熟悉斯威夫特

我正在做一个应用程序开发手册中的练习。我正在尝试创建一个
UIImageView
并在其中放置图像,但无法放大图像。我的代码有问题,但我不知道发生了什么

import UIKit

class ViewController: UIViewController, UIScrollViewDelegate {


    @IBOutlet weak var image: UIImageView!
    @IBOutlet weak var scroll: UIScrollView!

    override func viewDidLoad() {
        super.viewDidLoad()
        scroll.delegate = ViewController()
        updateZoomFor(size:view.bounds.size)
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    private func viewForZooming(in scrollView: UIScrollView) -> UIImageView{
        return image
    }

    func updateZoomFor(size:CGSize){
        let widthScale = size.width / image.bounds.width
        let heightScale = size.height / image.bounds.height
        let scale = min(widthScale, heightScale)
        scroll.minimumZoomScale = scale
    }

}

首先,按照3stud1ant3的建议设置
scroll.delegate=self

其次,您需要匹配
UIScrollViewDelegate
方法的定义:

func viewForZooming(in scrollView: UIScrollView) -> UIView? {
  return image
}
确保在
updateZoomFor
中设置缩放比例:
scroll.zoomScale=scale

您还需要实现以下功能:

override func viewWillLayoutSubviews() {  
  super.viewWillLayoutSubviews()
  updateZoomFor(size: view.bounds.size)
}

func scrollViewDidZoom(_ scrollView: UIScrollView) {
  updateConstraintsForSize(view.bounds.size)
}

fileprivate func updateConstraintsForSize(_ size: CGSize) {

  let yOffset = max(0, (size.height - imageView.frame.height) / 2)
  imageViewTopConstraint.constant = yOffset
  imageViewBottomConstraint.constant = yOffset

  let xOffset = max(0, (size.width - imageView.frame.width) / 2)
  imageViewLeadingConstraint.constant = xOffset
  imageViewTrailingConstraint.constant = xOffset

  view.layoutIfNeeded()
}

检查UIImageView的isUserInteractionEnabled属性是否为true,而不是
scroll.delegate=ViewController()
try
scroll.delegate=self
遵循本教程:最后一行有错误。所以我需要先定义updateMinZoomScaleForSize(view.bounds.size)吗?我还运行了没有最后一行的脚本。当我在模拟器中放大图像时,它会恢复到图像的原始大小。这是正常情况吗?好的。我已将答案编辑为使用您的
updateZoomFor
函数。再试一次,确保将约束连接为
IBOutlet
s。在运行模拟器时,imageViewLeadingConstraint、TrailingConstraint、BottomConstraint和TopConstraint存在一些问题。我需要先定义它们吗?是的。因此,首先在Interface builder中添加约束。然后按住ctrl键并将每个约束拖动到视图控制器中,以将它们添加为具有我上面提供的相同名称的插座。