2018-09-02 20:59:20 +00:00
|
|
|
//
|
|
|
|
// LargeImageViewController.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 8/31/18.
|
|
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2019-06-15 00:23:03 +00:00
|
|
|
import Pachyderm
|
2018-09-02 20:59:20 +00:00
|
|
|
import Photos
|
2018-11-09 20:48:08 +00:00
|
|
|
import Gifu
|
2018-09-02 20:59:20 +00:00
|
|
|
|
|
|
|
class LargeImageViewController: UIViewController, UIScrollViewDelegate {
|
|
|
|
|
2019-09-10 16:25:50 +00:00
|
|
|
typealias SourceInfo = (image: UIImage, frame: CGRect, cornerRadius: CGFloat)
|
2019-06-17 02:39:46 +00:00
|
|
|
|
|
|
|
var sourceInfo: SourceInfo?
|
2018-09-02 20:59:20 +00:00
|
|
|
var dismissInteractionController: LargeImageInteractionController?
|
|
|
|
|
|
|
|
@IBOutlet weak var scrollView: UIScrollView!
|
2018-11-09 20:48:08 +00:00
|
|
|
@IBOutlet weak var imageView: GIFImageView!
|
2018-09-02 20:59:20 +00:00
|
|
|
@IBOutlet weak var imageViewLeadingConstraint: NSLayoutConstraint!
|
|
|
|
@IBOutlet weak var imageViewTrailingConstraint: NSLayoutConstraint!
|
|
|
|
@IBOutlet weak var imageViewTopConstraint: NSLayoutConstraint!
|
|
|
|
@IBOutlet weak var imageViewBottomConstraint: NSLayoutConstraint!
|
|
|
|
|
|
|
|
@IBOutlet weak var topControlsView: UIView!
|
|
|
|
@IBOutlet weak var topControlsHeightConstraint: NSLayoutConstraint!
|
2019-06-15 00:10:17 +00:00
|
|
|
@IBOutlet weak var shareButton: UIButton!
|
|
|
|
@IBOutlet weak var shareButtonTopConstraint: NSLayoutConstraint!
|
|
|
|
@IBOutlet weak var shareButtonLeadingConstraint: NSLayoutConstraint!
|
2018-09-02 20:59:20 +00:00
|
|
|
@IBOutlet weak var closeButton: UIButton!
|
|
|
|
@IBOutlet weak var closeButtonTopConstraint: NSLayoutConstraint!
|
|
|
|
@IBOutlet weak var closeButtonTrailingConstraint: NSLayoutConstraint!
|
|
|
|
|
|
|
|
@IBOutlet weak var bottomControlsView: UIView!
|
|
|
|
@IBOutlet weak var descriptionLabel: UILabel!
|
|
|
|
|
2018-09-13 02:29:58 +00:00
|
|
|
var initializedTopControlsConstrains = false
|
|
|
|
|
2018-09-02 20:59:20 +00:00
|
|
|
var image: UIImage?
|
2018-11-09 20:48:08 +00:00
|
|
|
var gifData: Data?
|
2018-09-02 20:59:20 +00:00
|
|
|
var imageDescription: String?
|
|
|
|
|
2019-06-15 00:23:03 +00:00
|
|
|
var initialControlsVisible = true
|
|
|
|
private(set) var controlsVisible = true {
|
2018-09-02 20:59:20 +00:00
|
|
|
didSet {
|
2019-06-14 21:46:19 +00:00
|
|
|
setNeedsUpdateOfHomeIndicatorAutoHidden()
|
2018-09-02 20:59:20 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-15 00:23:03 +00:00
|
|
|
var shrinkGestureEnabled = true
|
2018-09-02 20:59:20 +00:00
|
|
|
|
|
|
|
var prevZoomScale: CGFloat?
|
|
|
|
|
|
|
|
override var prefersStatusBarHidden: Bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-06-14 21:46:19 +00:00
|
|
|
override var prefersHomeIndicatorAutoHidden: Bool {
|
|
|
|
return !controlsVisible
|
|
|
|
}
|
|
|
|
|
2019-06-17 02:39:46 +00:00
|
|
|
init(image: UIImage, description: String?, sourceInfo: SourceInfo?) {
|
2018-10-20 16:03:18 +00:00
|
|
|
self.image = image
|
|
|
|
self.imageDescription = description
|
2019-06-17 02:39:46 +00:00
|
|
|
self.sourceInfo = sourceInfo
|
|
|
|
|
2018-10-21 02:07:04 +00:00
|
|
|
super.init(nibName: "LargeImageViewController", bundle: nil)
|
2019-06-15 00:23:03 +00:00
|
|
|
|
|
|
|
modalPresentationStyle = .fullScreen
|
2018-10-20 16:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
2018-09-02 20:59:20 +00:00
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
2019-06-15 00:23:03 +00:00
|
|
|
setControlsVisible(initialControlsVisible, animated: false)
|
|
|
|
|
2018-09-02 20:59:20 +00:00
|
|
|
imageView.image = image
|
2018-11-09 20:48:08 +00:00
|
|
|
if let gifData = gifData {
|
|
|
|
imageView.animate(withGIFData: gifData)
|
|
|
|
}
|
|
|
|
|
2018-09-02 20:59:20 +00:00
|
|
|
scrollView.delegate = self
|
2018-11-09 20:48:08 +00:00
|
|
|
imageView.bounds = CGRect(origin: .zero, size: imageView.image!.size)
|
2018-09-02 20:59:20 +00:00
|
|
|
|
|
|
|
if let imageDescription = imageDescription {
|
|
|
|
descriptionLabel.text = imageDescription
|
|
|
|
} else {
|
|
|
|
bottomControlsView.isHidden = true
|
|
|
|
}
|
|
|
|
|
2019-06-15 00:23:03 +00:00
|
|
|
if shrinkGestureEnabled {
|
|
|
|
dismissInteractionController = LargeImageInteractionController(viewController: self)
|
|
|
|
}
|
2018-10-20 16:03:18 +00:00
|
|
|
|
|
|
|
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(scrollViewPressed(_:))))
|
|
|
|
let doubleTap = UITapGestureRecognizer(target: self, action: #selector(scrollViewDoubleTapped(_:)))
|
|
|
|
doubleTap.numberOfTapsRequired = 2
|
|
|
|
view.addGestureRecognizer(doubleTap)
|
2018-09-02 20:59:20 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 02:29:58 +00:00
|
|
|
|
2018-09-02 20:59:20 +00:00
|
|
|
override func viewDidLayoutSubviews() {
|
2018-09-13 02:29:58 +00:00
|
|
|
super.viewDidLayoutSubviews()
|
|
|
|
|
2019-06-14 22:27:17 +00:00
|
|
|
// limit the image height to the safe area height, so the image doesn't overlap the top controls
|
|
|
|
// while zoomed all the way out
|
|
|
|
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
|
2018-09-02 20:59:20 +00:00
|
|
|
let minScale = min(widthScale, heightScale)
|
|
|
|
scrollView.minimumZoomScale = minScale
|
|
|
|
scrollView.zoomScale = minScale
|
|
|
|
scrollView.maximumZoomScale = minScale >= 1 ? minScale + 2 : 2
|
2018-09-13 02:29:58 +00:00
|
|
|
|
2018-10-31 02:14:02 +00:00
|
|
|
centerImage()
|
|
|
|
|
2018-09-13 02:29:58 +00:00
|
|
|
if !initializedTopControlsConstrains {
|
|
|
|
initializedTopControlsConstrains = true
|
|
|
|
if view.safeAreaInsets.top == 44 {
|
|
|
|
// running on iPhone X style notched device
|
|
|
|
let notchWidth: CGFloat = 209
|
|
|
|
let earWidth = (view.bounds.width - notchWidth) / 2
|
2019-06-15 00:10:17 +00:00
|
|
|
let offset = (earWidth - shareButton.bounds.width) / 2
|
|
|
|
shareButtonLeadingConstraint.constant = offset
|
2018-09-13 02:29:58 +00:00
|
|
|
closeButtonTrailingConstraint.constant = offset
|
|
|
|
}
|
|
|
|
}
|
2018-09-02 20:59:20 +00:00
|
|
|
}
|
2019-06-15 00:23:03 +00:00
|
|
|
|
|
|
|
func setControlsVisible(_ controlsVisible: Bool, animated: Bool) {
|
|
|
|
self.controlsVisible = controlsVisible
|
|
|
|
if animated {
|
|
|
|
UIView.animate(withDuration: 0.2) {
|
|
|
|
self.updateControlsView()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
updateControlsView()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateControlsView() {
|
|
|
|
let topOffset = self.controlsVisible ? 0 : -self.topControlsView.bounds.height
|
|
|
|
self.topControlsView.transform = CGAffineTransform(translationX: 0, y: topOffset)
|
|
|
|
if self.imageDescription != nil {
|
|
|
|
let bottomOffset = self.controlsVisible ? 0 : self.bottomControlsView.bounds.height + self.view.safeAreaInsets.bottom
|
|
|
|
self.bottomControlsView.transform = CGAffineTransform(translationX: 0, y: bottomOffset)
|
|
|
|
}
|
|
|
|
}
|
2018-09-02 20:59:20 +00:00
|
|
|
|
|
|
|
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
|
|
|
|
return imageView
|
|
|
|
}
|
|
|
|
|
|
|
|
func scrollViewDidZoom(_ scrollView: UIScrollView) {
|
|
|
|
centerImage()
|
|
|
|
|
|
|
|
let prevZoomScale = self.prevZoomScale ?? scrollView.minimumZoomScale
|
|
|
|
if scrollView.zoomScale <= scrollView.minimumZoomScale {
|
2019-11-29 02:51:24 +00:00
|
|
|
setControlsVisible(true, animated: true)
|
2018-09-02 20:59:20 +00:00
|
|
|
} else if scrollView.zoomScale > prevZoomScale {
|
2019-11-29 02:51:24 +00:00
|
|
|
setControlsVisible(false, animated: true)
|
2018-09-02 20:59:20 +00:00
|
|
|
}
|
|
|
|
self.prevZoomScale = scrollView.zoomScale
|
|
|
|
}
|
|
|
|
|
|
|
|
func centerImage() {
|
|
|
|
let yOffset = max(0, (view.bounds.size.height - imageView.frame.height) / 2)
|
|
|
|
imageViewTopConstraint.constant = yOffset
|
2019-07-29 15:38:57 +00:00
|
|
|
|
2018-09-02 20:59:20 +00:00
|
|
|
let xOffset = max(0, (view.bounds.size.width - imageView.frame.width) / 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()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-20 16:03:18 +00:00
|
|
|
@objc func scrollViewPressed(_ sender: UITapGestureRecognizer) {
|
2018-09-02 20:59:20 +00:00
|
|
|
if scrollView.zoomScale > scrollView.minimumZoomScale {
|
|
|
|
animateZoomOut()
|
|
|
|
} else {
|
2019-06-15 00:23:03 +00:00
|
|
|
setControlsVisible(!controlsVisible, animated: true)
|
2018-09-02 20:59:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-20 16:03:18 +00:00
|
|
|
@objc func scrollViewDoubleTapped(_ recognizer: UITapGestureRecognizer) {
|
2018-09-02 20:59:20 +00:00
|
|
|
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) {
|
2018-10-21 02:07:04 +00:00
|
|
|
dismiss(animated: true)
|
2018-09-02 20:59:20 +00:00
|
|
|
}
|
|
|
|
|
2019-06-15 00:10:17 +00:00
|
|
|
@IBAction func sharePressed(_ sender: Any) {
|
2018-09-02 20:59:20 +00:00
|
|
|
guard let image = image else { return }
|
2019-06-15 00:10:17 +00:00
|
|
|
let activityVC = UIActivityViewController(activityItems: [image], applicationActivities: nil)
|
|
|
|
present(activityVC, animated: true)
|
2018-09-02 20:59:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|