2019-10-12 21:45:44 +02:00
|
|
|
//
|
|
|
|
// ImageViewController.swift
|
|
|
|
// NetNewsWire-iOS
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 10/12/19.
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
|
|
|
class ImageViewController: UIViewController {
|
|
|
|
|
2020-03-12 21:54:50 +01:00
|
|
|
|
|
|
|
@IBOutlet weak var closeButton: UIButton!
|
2019-10-12 22:27:38 +02:00
|
|
|
@IBOutlet weak var shareButton: UIButton!
|
2019-10-12 21:45:44 +02:00
|
|
|
@IBOutlet weak var imageScrollView: ImageScrollView!
|
2020-01-01 02:06:27 +01:00
|
|
|
@IBOutlet weak var titleLabel: UILabel!
|
2020-03-12 21:23:57 +01:00
|
|
|
@IBOutlet weak var titleBackground: UIVisualEffectView!
|
|
|
|
@IBOutlet weak var titleLeading: NSLayoutConstraint!
|
|
|
|
@IBOutlet weak var titleTrailing: NSLayoutConstraint!
|
2019-10-12 21:45:44 +02:00
|
|
|
|
2019-10-14 02:41:34 +02:00
|
|
|
var image: UIImage!
|
2020-01-01 02:06:27 +01:00
|
|
|
var imageTitle: String?
|
2019-10-16 01:08:13 +02:00
|
|
|
var zoomedFrame: CGRect {
|
|
|
|
return imageScrollView.zoomedFrame
|
|
|
|
}
|
2019-10-12 21:45:44 +02:00
|
|
|
|
2023-04-22 14:13:41 +02:00
|
|
|
override var keyCommands: [UIKeyCommand]? {
|
|
|
|
return [
|
|
|
|
UIKeyCommand(
|
|
|
|
title: NSLocalizedString("Close Image", comment: "Close Image"),
|
|
|
|
action: #selector(done(_:)),
|
|
|
|
input: " "
|
|
|
|
)
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2019-10-12 21:45:44 +02:00
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
2020-03-12 21:54:50 +01:00
|
|
|
closeButton.imageView?.contentMode = .scaleAspectFit
|
2020-04-21 18:05:26 +02:00
|
|
|
closeButton.accessibilityLabel = NSLocalizedString("Close", comment: "Close")
|
|
|
|
shareButton.accessibilityLabel = NSLocalizedString("Share", comment: "Share")
|
2020-03-12 21:54:50 +01:00
|
|
|
|
2019-10-12 21:45:44 +02:00
|
|
|
imageScrollView.setup()
|
|
|
|
imageScrollView.imageScrollViewDelegate = self
|
|
|
|
imageScrollView.imageContentMode = .aspectFit
|
|
|
|
imageScrollView.initialOffset = .center
|
2019-10-14 02:41:34 +02:00
|
|
|
imageScrollView.display(image: image)
|
2020-01-01 02:06:27 +01:00
|
|
|
|
|
|
|
titleLabel.text = imageTitle ?? ""
|
2020-03-12 21:23:57 +01:00
|
|
|
layoutTitleLabel()
|
|
|
|
|
2020-03-12 21:33:02 +01:00
|
|
|
guard imageTitle != "" else {
|
2020-03-12 21:23:57 +01:00
|
|
|
titleBackground.removeFromSuperview()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
titleBackground.layer.cornerRadius = 6
|
2019-10-12 21:45:44 +02:00
|
|
|
}
|
|
|
|
|
2019-10-17 03:20:36 +02:00
|
|
|
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
|
|
|
|
super.viewWillTransition(to: size, with: coordinator)
|
|
|
|
coordinator.animate(alongsideTransition: { [weak self] context in
|
|
|
|
self?.imageScrollView.resize()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-12 22:27:38 +02:00
|
|
|
@IBAction func share(_ sender: Any) {
|
|
|
|
guard let image = image else { return }
|
2019-10-14 02:41:34 +02:00
|
|
|
let activityViewController = UIActivityViewController(activityItems: [image], applicationActivities: nil)
|
2019-10-12 22:27:38 +02:00
|
|
|
activityViewController.popoverPresentationController?.sourceView = shareButton
|
2019-10-12 23:54:24 +02:00
|
|
|
activityViewController.popoverPresentationController?.sourceRect = shareButton.bounds
|
2019-10-12 22:27:38 +02:00
|
|
|
present(activityViewController, animated: true)
|
|
|
|
}
|
|
|
|
|
2019-10-12 21:45:44 +02:00
|
|
|
@IBAction func done(_ sender: Any) {
|
|
|
|
dismiss(animated: true)
|
|
|
|
}
|
|
|
|
|
2020-03-12 21:23:57 +01:00
|
|
|
private func layoutTitleLabel(){
|
|
|
|
let width = view.frame.width
|
|
|
|
let multiplier = UIDevice.current.userInterfaceIdiom == .pad ? CGFloat(0.1) : CGFloat(0.04)
|
|
|
|
titleLeading.constant += width * multiplier
|
|
|
|
titleTrailing.constant -= width * multiplier
|
|
|
|
titleLabel.layoutIfNeeded()
|
|
|
|
}
|
2019-10-12 21:45:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: ImageScrollViewDelegate
|
|
|
|
|
|
|
|
extension ImageViewController: ImageScrollViewDelegate {
|
|
|
|
|
|
|
|
func imageScrollViewDidGestureSwipeUp(imageScrollView: ImageScrollView) {
|
|
|
|
dismiss(animated: true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func imageScrollViewDidGestureSwipeDown(imageScrollView: ImageScrollView) {
|
|
|
|
dismiss(animated: true)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|