滚动视图中的ImageView放大swift3失败

时间:2017-05-22 01:09:31

标签: ios swift uiscrollview uiimageview uiscrollviewdelegate

抱歉,我是swift的新手,我有一个imageView。

我可以在imageView中看到我的图像 我希望能够放大和缩小图像。但我也无法改变我的图像大小。我的代码出了什么问题?

很抱歉,我已经找到了很多方法来解决我的问题。我不知道如何解决它。我应该添加一些gestureRecognizer或其他什么吗? 谢谢你的回答。

这是代码:

import UIKit
import SnapKit

class ImageViewController: UIViewController, UIScrollViewDelegate {

var url:String = ""
var id:String = ""

var scrollView:UIScrollView = { () -> UIScrollView in
    let ui = UIScrollView()
    ui.backgroundColor = UIColor.black
    ui.minimumZoomScale = 1.0
    ui.maximumZoomScale = 6.0
    return ui
}()

var toolbar:UIToolbar = { () -> UIToolbar in
    let ui = UIToolbar()
    ui.barTintColor = defaultNavBackgroundColor
    ui.clipsToBounds = true
    ui.isTranslucent = false
    ui.isUserInteractionEnabled = true
    return ui
}()

var photoImageView:UIImageView = { () -> UIImageView in
    let ui = UIImageView()
    ui.backgroundColor = UIColor.clear
    ui.layer.masksToBounds = true
    ui.contentMode = .scaleAspectFit
    ui.isUserInteractionEnabled = true
    return ui
}()

var buttonDownload:UIBarButtonItem = { () -> UIBarButtonItem in
    let ui = UIBarButtonItem()
    ui.style = .plain
    return ui
}()

override func loadView() {
    super.loadView()

    view.backgroundColor = UIColor.black
}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
}

override func viewDidLoad() {
    super.viewDidLoad()

    // navigationItem.title = name

    self.automaticallyAdjustsScrollViewInsets = false

    imageFromUrl(imageView: photoImageView, url: url, chatroomId: id,isResize: false)

    self.buttonDownload.action = #selector(btnDownloadClicked(sender:))

    scrollView.delegate = self
    scrollView.contentSize = (self.photoImageView.image!.size)

    photoImageView.center = scrollView.center

    loadContent()
    loadVFL()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

func loadContent() {

    toolbar.sizeToFit()
    let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
    buttonDownload = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: nil)
    toolbar.items = [flexSpace,buttonDownload]

    view.addSubview(scrollView)
    view.addSubview(toolbar)
    scrollView.addSubview(photoImageView)
}

func loadVFL() {

    scrollView.snp.makeConstraints { (make) -> Void in
        make.top.equalToSuperview()
        make.bottom.equalTo(toolbar.snp.top)
        make.left.equalToSuperview()
        make.right.equalToSuperview()
    }

    photoImageView.snp.makeConstraints { (make) -> Void in
        make.top.equalToSuperview()
        make.bottom.equalToSuperview()
        make.left.equalToSuperview()
        make.right.equalToSuperview()
        make.centerX.equalToSuperview()
        make.centerY.equalToSuperview()
    }

    toolbar.snp.makeConstraints { (make) -> Void in
        make.width.equalToSuperview()
        make.bottom.equalToSuperview()
        make.height.equalTo(44)
    }
}

func btnDownloadClicked(sender:UIBarButtonItem) {
    print("press")
}

// MARK: - UIScrollViewDelegate
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
    return photoImageView
}
}

image like this

0 个答案:

没有答案