Files
NetNewsWire/iOS/Article/ImageViewController.swift
T

98 lines
2.8 KiB
Swift
Raw Normal View History

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
final class ImageViewController: UIViewController {
2025-11-19 19:43:09 -08:00
@IBOutlet var closeButton: UIButton!
@IBOutlet var shareButton: UIButton!
@IBOutlet var imageScrollView: ImageScrollView!
@IBOutlet var titleLabel: UILabel!
@IBOutlet var titleBackground: UIVisualEffectView!
@IBOutlet var titleLeading: NSLayoutConstraint!
@IBOutlet var titleTrailing: NSLayoutConstraint!
2025-11-05 11:22:10 -08:00
var image: UIImage!
var imageTitle: String?
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
2020-03-12 21:54:50 +01:00
closeButton.imageView?.contentMode = .scaleAspectFit
closeButton.accessibilityLabel = NSLocalizedString("Close", comment: "Close")
shareButton.accessibilityLabel = NSLocalizedString("Share", comment: "Share")
2025-11-05 11:22:10 -08:00
2019-10-12 14:45:44 -05:00
imageScrollView.setup()
imageScrollView.imageScrollViewDelegate = self
imageScrollView.imageContentMode = .aspectFit
imageScrollView.initialOffset = .center
imageScrollView.display(image: image)
2025-11-05 11:22:10 -08:00
titleLabel.text = imageTitle ?? ""
layoutTitleLabel()
2025-11-05 11:22:10 -08:00
2020-03-12 21:33:02 +01:00
guard imageTitle != "" else {
titleBackground.removeFromSuperview()
return
}
titleBackground.layer.cornerRadius = 6
2019-10-12 14:45:44 -05:00
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
2026-01-02 17:50:35 -08:00
coordinator.animate(alongsideTransition: { [weak self] _ in
self?.imageScrollView.resize()
})
}
2025-11-05 11:22:10 -08:00
2019-10-12 15:27:38 -05:00
@IBAction func share(_ sender: Any) {
guard let image = image else { return }
let activityViewController = UIActivityViewController(activityItems: [image], applicationActivities: nil)
2019-10-12 15:27:38 -05:00
activityViewController.popoverPresentationController?.sourceView = shareButton
2019-10-12 16:54:24 -05:00
activityViewController.popoverPresentationController?.sourceRect = shareButton.bounds
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() {
let width = view.frame.width
2025-07-23 21:58:07 +08:00
let multiplier = traitCollection.userInterfaceIdiom == .pad ? CGFloat(0.1) : CGFloat(0.04)
titleLeading.constant += width * multiplier
titleTrailing.constant -= width * multiplier
titleLabel.layoutIfNeeded()
}
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)
}
}