Skip to content

Instantly share code, notes, and snippets.

@alexpaul
Created May 22, 2018 01:10
Show Gist options
  • Save alexpaul/fadee12bc5ee0ddac60af1d876d544c5 to your computer and use it in GitHub Desktop.
Save alexpaul/fadee12bc5ee0ddac60af1d876d544c5 to your computer and use it in GitHub Desktop.
A UIScrollView with an UIImageView that enables zooming.
import UIKit
import Kingfisher
class FullScreenImageView: UIView {
@IBOutlet var contentView: UIView!
@IBOutlet var dismissButton: UIButton!
@IBOutlet var shareButton: UIButton!
@IBOutlet var imageView: UIImageView!
@IBOutlet var scrollView: UIScrollView!
@IBOutlet var imageViewTopConstraint: NSLayoutConstraint!
@IBOutlet var imageViewBottomConstraint: NSLayoutConstraint!
@IBOutlet var imageViewLeadingConstraint: NSLayoutConstraint!
@IBOutlet var imageViewTrailingConstraint: NSLayoutConstraint!
override init(frame: CGRect) {
super.init(frame: UIScreen.main.bounds)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
fileprivate func commonInit() {
Bundle.main.loadNibNamed("FullScreenImageView", owner: self, options: nil)
addSubview(contentView)
contentView.frame = bounds
contentView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
imageView.contentMode = .scaleAspectFit
shareButton.setImage(UIImage(named: "shareIcon"), for: .normal)
scrollView.delegate = self
scrollView.maximumZoomScale = 6.0
scrollView.minimumZoomScale = 1.0
}
public func configureView(_ photo: Photo) {
guard let urlString = photo.url_m else {
imageView.image = UIImage(named: "placeholderImage")
return
}
if let image = ImageCache.default.retrieveImageInDiskCache(forKey: urlString) {
imageView.image = image
} else if let image = ImageCache.default.retrieveImageInMemoryCache(forKey: urlString) {
imageView.image = image
}
}
}
extension FullScreenImageView: UIScrollViewDelegate {
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imageView
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment