150 lines
5.1 KiB
Swift
150 lines
5.1 KiB
Swift
//
|
|
// LargeImageViewController.swift
|
|
// Tusker
|
|
//
|
|
// Created by Shadowfacts on 8/31/18.
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
|
|
class LargeImageViewController: UIViewController, UIScrollViewDelegate {
|
|
|
|
@IBOutlet weak var scrollView: UIScrollView!
|
|
@IBOutlet weak var imageView: UIImageView!
|
|
@IBOutlet weak var imageViewLeadingConstraint: NSLayoutConstraint!
|
|
@IBOutlet weak var imageViewTopConstraint: NSLayoutConstraint!
|
|
|
|
var image: UIImage? {
|
|
didSet {
|
|
imageView.image = image
|
|
if oldValue == nil, let image = image {
|
|
imageView.bounds = CGRect(origin: .zero, size: image.size)
|
|
view.setNeedsLayout()
|
|
}
|
|
}
|
|
}
|
|
|
|
var prevZoomScale: CGFloat?
|
|
|
|
init() {
|
|
super.init(nibName: "LargeImageViewController", bundle: nil)
|
|
}
|
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
// imageView.image = image
|
|
|
|
scrollView.delegate = self
|
|
// imageView.bounds = CGRect(origin: .zero, size: imageView.image!.size)
|
|
|
|
// view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(scrollViewPressed(_:))))
|
|
let doubleTap = UITapGestureRecognizer(target: self, action: #selector(scrollViewDoubleTapped(_:)))
|
|
doubleTap.numberOfTapsRequired = 2
|
|
view.addGestureRecognizer(doubleTap)
|
|
}
|
|
|
|
|
|
override func viewDidLayoutSubviews() {
|
|
super.viewDidLayoutSubviews()
|
|
|
|
let maxHeight = view.bounds.height - view.safeAreaInsets.top - view.safeAreaInsets.bottom
|
|
let heightScale = maxHeight / imageView.bounds.height
|
|
let widthScale = view.bounds.width / imageView.bounds.width
|
|
let minScale = min(widthScale, heightScale)
|
|
scrollView.minimumZoomScale = minScale
|
|
scrollView.zoomScale = minScale
|
|
scrollView.maximumZoomScale = minScale * 4
|
|
|
|
centerImage()
|
|
}
|
|
|
|
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
|
|
return imageView
|
|
}
|
|
|
|
func scrollViewDidZoom(_ scrollView: UIScrollView) {
|
|
centerImage()
|
|
|
|
// let prevZoomScale = self.prevZoomScale ?? scrollView.minimumZoomScale
|
|
// if scrollView.zoomScale <= scrollView.minimumZoomScale {
|
|
// controlsVisible = true
|
|
// } else if scrollView.zoomScale > prevZoomScale {
|
|
// controlsVisible = false
|
|
// }
|
|
// self.prevZoomScale = scrollView.zoomScale
|
|
}
|
|
|
|
func centerImage() {
|
|
let yOffset = max(0, (view.bounds.size.height - imageView.frame.height - scrollView.contentInset.top - scrollView.contentInset.bottom) / 2)
|
|
imageViewTopConstraint.constant = yOffset
|
|
|
|
let xOffset = max(0, (view.bounds.size.width - imageView.frame.width - scrollView.contentInset.left - scrollView.contentInset.right) / 2)
|
|
imageViewLeadingConstraint.constant = xOffset
|
|
}
|
|
|
|
func zoomRectFor(scale: CGFloat, center: CGPoint) -> CGRect {
|
|
var zoomRect = CGRect.zero
|
|
zoomRect.size.width = imageView.frame.width / scale
|
|
zoomRect.size.height = imageView.frame.height / scale
|
|
let newCenter = scrollView.convert(center, to: imageView)
|
|
zoomRect.origin.x = newCenter.x - (zoomRect.width / 2)
|
|
zoomRect.origin.y = newCenter.y - (zoomRect.height / 2)
|
|
return zoomRect
|
|
}
|
|
|
|
func animateZoomOut() {
|
|
UIView.animate(withDuration: 0.3, animations: {
|
|
self.scrollView.zoomScale = self.scrollView.minimumZoomScale
|
|
self.view.layoutIfNeeded()
|
|
})
|
|
}
|
|
|
|
// @objc func scrollViewPressed(_ sender: UITapGestureRecognizer) {
|
|
// if scrollView.zoomScale > scrollView.minimumZoomScale {
|
|
// animateZoomOut()
|
|
// } else {
|
|
//// setControlsVisible(!controlsVisible, animated: true)
|
|
// }
|
|
// }
|
|
|
|
@objc func scrollViewDoubleTapped(_ recognizer: UITapGestureRecognizer) {
|
|
if scrollView.zoomScale <= scrollView.minimumZoomScale {
|
|
let point = recognizer.location(in: recognizer.view)
|
|
let scale: CGFloat
|
|
if scrollView.minimumZoomScale < 1 {
|
|
if 1 - scrollView.zoomScale <= 0.5 {
|
|
scale = scrollView.zoomScale + 1
|
|
} else {
|
|
scale = 1
|
|
}
|
|
} else {
|
|
scale = scrollView.maximumZoomScale
|
|
}
|
|
let rect = zoomRectFor(scale: scale, center: point)
|
|
UIView.animate(withDuration: 0.3) {
|
|
self.scrollView.zoom(to: rect, animated: false)
|
|
self.view.layoutIfNeeded()
|
|
}
|
|
} else {
|
|
animateZoomOut()
|
|
}
|
|
}
|
|
|
|
// @IBAction func closeButtonPressed(_ sender: Any) {
|
|
// dismiss(animated: true)
|
|
// }
|
|
//
|
|
// @IBAction func sharePressed(_ sender: Any) {
|
|
// guard let image = image else { return }
|
|
// let activityVC = UIActivityViewController(activityItems: [image], applicationActivities: nil)
|
|
// present(activityVC, animated: true)
|
|
// }
|
|
|
|
}
|