Last active
August 3, 2023 12:52
-
-
Save jonnytownend/0215af647596117960ef9a15ec7e39fa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
]) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private func commonInit() { | |
// Setup image view | |
// ... | |
// Setup scroll view | |
minimumZoomScale = 1 | |
maximumZoomScale = 3 | |
showsHorizontalScrollIndicator = false | |
showsVerticalScrollIndicator = false | |
delegate = self | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension PanZoomImageView: UIScrollViewDelegate { | |
func viewForZooming(in scrollView: UIScrollView) -> UIView? { | |
return imageView | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@IBInspectable | |
private var imageName: String? { | |
didSet { | |
guard let imageName = imageName else { | |
return | |
} | |
imageView.image = UIImage(named: imageName) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
convenience init(named: String) { | |
self.init(frame: .zero) | |
self.imageName = named | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let myView = PanZoomImageView(named: "image-name") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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