Skip to content

Instantly share code, notes, and snippets.

@jonnytownend
Last active August 3, 2023 12:52
Show Gist options
  • Save jonnytownend/0215af647596117960ef9a15ec7e39fa to your computer and use it in GitHub Desktop.
Save jonnytownend/0215af647596117960ef9a15ec7e39fa to your computer and use it in GitHub Desktop.
class PanZoomImageView: UIScrollView {
private let imageView = UIImageView()
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
private func commonInit() {
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFit
addSubview(imageView)
NSLayoutConstraint.activate([
imageView.widthAnchor.constraint(equalTo: widthAnchor),
imageView.heightAnchor.constraint(equalTo: heightAnchor),
imageView.centerXAnchor.constraint(equalTo: centerXAnchor),
imageView.centerYAnchor.constraint(equalTo: centerYAnchor)
])
}
}
private func commonInit() {
// Setup image view
// ...
// Setup scroll view
minimumZoomScale = 1
maximumZoomScale = 3
showsHorizontalScrollIndicator = false
showsVerticalScrollIndicator = false
delegate = self
}
extension PanZoomImageView: UIScrollViewDelegate {
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imageView
}
}
@IBInspectable
private var imageName: String? {
didSet {
guard let imageName = imageName else {
return
}
imageView.image = UIImage(named: imageName)
}
}
convenience init(named: String) {
self.init(frame: .zero)
self.imageName = named
}
let myView = PanZoomImageView(named: "image-name")
class PanZoomImageView: UIScrollView {
@IBInspectable
private var imageName: String? {
didSet {
guard let imageName = imageName else {
return
}
imageView.image = UIImage(named: imageName)
}
}
private let imageView = UIImageView()
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
convenience init(named: String) {
self.init(frame: .zero)
self.imageName = named
}
private func commonInit() {
// Setup image view
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFit
addSubview(imageView)
NSLayoutConstraint.activate([
imageView.widthAnchor.constraint(equalTo: widthAnchor),
imageView.heightAnchor.constraint(equalTo: heightAnchor),
imageView.centerXAnchor.constraint(equalTo: centerXAnchor),
imageView.centerYAnchor.constraint(equalTo: centerYAnchor)
])
// Setup scroll view
minimumZoomScale = 1
maximumZoomScale = 3
showsHorizontalScrollIndicator = false
showsVerticalScrollIndicator = false
delegate = self
}
}
extension PanZoomImageView: UIScrollViewDelegate {
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imageView
}
}
private func commonInit() {
// Setup image view
// ...
// Setup scroll view
// ...
// Setup tap gesture
let doubleTapRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap(_:)))
doubleTapRecognizer.numberOfTapsRequired = 2
addGestureRecognizer(doubleTapRecognizer)
}
@objc private func handleDoubleTap(_ sender: UITapGestureRecognizer) {
if zoomScale == 1 {
setZoomScale(2, animated: true)
} else {
setZoomScale(1, animated: true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment