Tusker/Tusker/Screens/Large Image/LargeImageViewController.swift

237 lines
8.5 KiB
Swift
Raw Normal View History

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
2018-11-09 20:48:08 +00:00
import Gifu
2018-09-02 20:59:20 +00:00
class LargeImageViewController: UIViewController, UIScrollViewDelegate, LargeImageAnimatableViewController {
2018-09-02 20:59:20 +00:00
weak var animationSourceView: UIImageView?
var animationImage: UIImage? { image ?? animationSourceView?.image }
var animationGifData: Data? { gifData }
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!
@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!
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 {
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
}
override var prefersHomeIndicatorAutoHidden: Bool {
return !controlsVisible
}
init(image: UIImage, description: String?, sourceView: UIImageView?) {
2018-10-20 16:03:18 +00:00
self.image = image
self.imageDescription = description
self.animationSourceView = sourceView
2019-06-17 02:39:46 +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-02 20:59:20 +00:00
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// 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
centerImage()
if view.safeAreaInsets.top == 44 {
// running on iPhone X style notched device
let notchWidth: CGFloat = 209
let earWidth = (view.bounds.width - notchWidth) / 2
let offset = (earWidth - shareButton.bounds.width) / 2
shareButtonLeadingConstraint.constant = offset
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 {
setControlsVisible(true, animated: true)
2018-09-02 20:59:20 +00:00
} else if scrollView.zoomScale > prevZoomScale {
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
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
}
// MARK: Interaction
2018-09-02 20:59:20 +00:00
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) {
dismiss(animated: true)
2018-09-02 20:59:20 +00:00
}
@IBAction func sharePressed(_ sender: Any) {
2018-09-02 20:59:20 +00:00
guard let image = image else { return }
let activityVC = UIActivityViewController(activityItems: [image], applicationActivities: nil)
if let presentationController = activityVC.presentationController as? UIPopoverPresentationController {
presentationController.sourceView = shareButton
}
present(activityVC, animated: true)
2018-09-02 20:59:20 +00:00
}
}