2019-10-12 14:45:44 -05:00
|
|
|
|
//
|
|
|
|
|
|
// ImageViewController.swift
|
|
|
|
|
|
// NetNewsWire-iOS
|
|
|
|
|
|
//
|
|
|
|
|
|
// Created by Maurice Parker on 10/12/19.
|
|
|
|
|
|
// Copyright © 2019 Ranchero Software. All rights reserved.
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
|
|
2025-09-16 10:21:29 -07:00
|
|
|
|
final class ImageViewController: UIViewController {
|
2025-11-19 19:43:09 -08:00
|
|
|
|
@IBOutlet var imageScrollView: ImageScrollView!
|
|
|
|
|
|
@IBOutlet var titleLabel: UILabel!
|
|
|
|
|
|
@IBOutlet var titleBackground: UIVisualEffectView!
|
|
|
|
|
|
@IBOutlet var titleLeading: NSLayoutConstraint!
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-05-27 11:38:09 -07:00
|
|
|
|
private var shareButtonItem: UIBarButtonItem?
|
|
|
|
|
|
|
2026-11-04 11:51:34 -08:00
|
|
|
|
// Title margins as a fraction of the view width.
|
|
|
|
|
|
private static let regularWidthTitleMarginMultiplier: CGFloat = 0.1
|
|
|
|
|
|
private static let compactWidthTitleMarginMultiplier: CGFloat = 0.04
|
|
|
|
|
|
|
2019-10-13 19:41:34 -05:00
|
|
|
|
var image: UIImage!
|
2019-12-31 18:06:27 -07:00
|
|
|
|
var imageTitle: String?
|
2026-08-02 16:57:28 -07:00
|
|
|
|
|
|
|
|
|
|
// Strong reference — the navigation controller’s transitioningDelegate is weak,
|
|
|
|
|
|
// and the transition must outlive the WebViewController that configured it.
|
|
|
|
|
|
var transition: ImageTransition?
|
2019-10-15 18:08:13 -05:00
|
|
|
|
var zoomedFrame: CGRect {
|
|
|
|
|
|
return imageScrollView.zoomedFrame
|
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2025-05-09 20:30:04 -07:00
|
|
|
|
override var keyCommands: [UIKeyCommand]? {
|
|
|
|
|
|
return [
|
|
|
|
|
|
UIKeyCommand(
|
|
|
|
|
|
title: NSLocalizedString("Close Image", comment: "Close Image"),
|
|
|
|
|
|
action: #selector(done(_:)),
|
|
|
|
|
|
input: " "
|
|
|
|
|
|
)
|
|
|
|
|
|
]
|
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2019-10-12 14:45:44 -05:00
|
|
|
|
override func viewDidLoad() {
|
|
|
|
|
|
super.viewDidLoad()
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-05-27 11:38:09 -07:00
|
|
|
|
let closeButtonItem = UIBarButtonItem(barButtonSystemItem: .close, target: self, action: #selector(done(_:)))
|
|
|
|
|
|
closeButtonItem.tintColor = Assets.Colors.primaryAccent
|
|
|
|
|
|
let shareButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(share(_:)))
|
|
|
|
|
|
shareButtonItem.tintColor = Assets.Colors.primaryAccent
|
|
|
|
|
|
navigationItem.leftBarButtonItem = closeButtonItem
|
|
|
|
|
|
navigationItem.rightBarButtonItem = shareButtonItem
|
|
|
|
|
|
self.shareButtonItem = shareButtonItem
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2019-10-12 14:45:44 -05:00
|
|
|
|
imageScrollView.setup()
|
2026-05-27 11:38:09 -07:00
|
|
|
|
// The image viewer is full-screen, so the scroll view ignores
|
|
|
|
|
|
// the navigation bar and safe area insets. Otherwise the image is pushed down
|
|
|
|
|
|
// and doesn’t match the zoom transition’s target frame.
|
|
|
|
|
|
imageScrollView.contentInsetAdjustmentBehavior = .never
|
2019-10-12 14:45:44 -05:00
|
|
|
|
imageScrollView.imageScrollViewDelegate = self
|
|
|
|
|
|
imageScrollView.imageContentMode = .aspectFit
|
|
|
|
|
|
imageScrollView.initialOffset = .center
|
2019-10-13 19:41:34 -05:00
|
|
|
|
imageScrollView.display(image: image)
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2019-12-31 18:06:27 -07:00
|
|
|
|
titleLabel.text = imageTitle ?? ""
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
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 14:45:44 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-11-04 11:52:23 -08:00
|
|
|
|
override func viewWillLayoutSubviews() {
|
|
|
|
|
|
super.viewWillLayoutSubviews()
|
|
|
|
|
|
layoutTitleLabel()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-10-12 15:27:38 -05:00
|
|
|
|
@IBAction func share(_ sender: Any) {
|
2026-05-27 11:38:09 -07:00
|
|
|
|
guard let image else {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2019-10-13 19:41:34 -05:00
|
|
|
|
let activityViewController = UIActivityViewController(activityItems: [image], applicationActivities: nil)
|
2026-05-27 11:38:09 -07:00
|
|
|
|
activityViewController.popoverPresentationController?.barButtonItem = shareButtonItem
|
2019-10-12 15:27:38 -05:00
|
|
|
|
present(activityViewController, animated: true)
|
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2019-10-12 14:45:44 -05:00
|
|
|
|
@IBAction func done(_ sender: Any) {
|
|
|
|
|
|
dismiss(animated: true)
|
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-01-02 17:50:35 -08:00
|
|
|
|
private func layoutTitleLabel() {
|
2026-11-04 11:51:34 -08:00
|
|
|
|
let multiplier = traitCollection.horizontalSizeClass == .regular ? Self.regularWidthTitleMarginMultiplier : Self.compactWidthTitleMarginMultiplier
|
2026-11-04 11:54:40 -08:00
|
|
|
|
titleLeading.constant = view.bounds.width * multiplier
|
2020-03-12 21:23:57 +01:00
|
|
|
|
}
|
2019-10-12 14:45:44 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// MARK: ImageScrollViewDelegate
|
|
|
|
|
|
|
|
|
|
|
|
extension ImageViewController: ImageScrollViewDelegate {
|
|
|
|
|
|
|
|
|
|
|
|
func imageScrollViewDidGestureSwipeUp(imageScrollView: ImageScrollView) {
|
|
|
|
|
|
dismiss(animated: true)
|
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2019-10-12 14:45:44 -05:00
|
|
|
|
func imageScrollViewDidGestureSwipeDown(imageScrollView: ImageScrollView) {
|
|
|
|
|
|
dismiss(animated: true)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|