Reputation: 97
I have added a tap gesture recognizer action to my image view. Once the image is tapped it expands to full screen and then dismisses full screen once tapped again. How would I add pinch to zoom to the image after it is already expanded. Here is my code for making the image full screen.
//expand image
let newImageView: UIImageView!
@IBAction func imageTapped(_ sender: UITapGestureRecognizer)
{
let imageView = sender.view as! UIImageView
let scrollView = UIScrollView(frame: self.view.frame)
let newImageView = UIImageView(image: imageView.image)
newImageView.frame = self.view.frame
newImageView.backgroundColor = .black
newImageView.contentMode = .scaleAspectFit
newImageView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage))
scrollView.addGestureRecognizer(tap)
scrollView.delegate = self
scrollView.minimumZoomScale = 1.0
scrollView.maximumZoomScale = 5.0
scrollView.addSubview(newImageView)
self.view.addSubview(scrollView)
}
func viewForZooming(in scrollView: UIScrollView) -> UIView?
{
return newImageView;
}
func dismissFullscreenImage(_ sender: UITapGestureRecognizer) {
sender.view?.removeFromSuperview()
}
Upvotes: 1
Views: 783
Reputation: 1142
When you wanna show full image, let use scrollview
instead imageView
let newImageView: UIImageView!
@IBAction func imageTapped(_ sender: UITapGestureRecognizer)
{
let imageView = sender.view as! UIImageView
let scrollView = UIScrollView(frame: self.view.frame);
newImageView = UIImageView(image: imageView.image)
newImageView.frame = self.view.frame
newImageView.backgroundColor = .black
newImageView.contentMode = .scaleAspectFit
newImageView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage))
scrollView.addGestureRecognizer(tap)
scrollView.delegate = self
scrollView.minimumZoomScale = 1.0
scrollView.maximumZoomScale = 5.0
scrollView.addSubview(newImageView)
self.view.addSubview(scrollView)
}
and remember viewForZooming
delegate func
func viewForZooming(in scrollView: UIScrollView) -> UIView?
{
return newImageView;
}
Upvotes: 1
Reputation: 11
if you want to zoom the imageView, you should add the imageView on a UIScrollView and add the method below:
- (CGRect)zoomRectForScrollView:(UIScrollView *)scrollView withScale:(float)scale withCenter:(CGPoint)center {
CGRect zoomRect;
// The zoom rect is in the content view's coordinates.
// At a zoom scale of 1.0, it would be the size of the
// imageScrollView's bounds.
// As the zoom scale decreases, so more content is visible,
// the size of the rect grows.
zoomRect.size.height = scrollView.frame.size.height / scale;
zoomRect.size.width = scrollView.frame.size.width / scale;
// choose an origin so as to get the right center.
zoomRect.origin.x = center.x - (zoomRect.size.width / 2.0);
zoomRect.origin.y = center.y - (zoomRect.size.height / 2.0);
return zoomRect;
}
for more detail :https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/UIScrollView_pg/ZoomZoom/ZoomZoom.html
Upvotes: 0