简体   繁体   中英

Swift- Fullscreen image with user interaction

I realise this question has already been answered but the answer I found here:

@IBAction func imageTapped(_ sender: UITapGestureRecognizer) {
let imageView = sender.view as! UIImageView
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))
newImageView.addGestureRecognizer(tap)
self.view.addSubview(newImageView)
}

func dismissFullscreenImage(_ sender: UITapGestureRecognizer) {
sender.view?.removeFromSuperview()
}

Didn't produce any errors but didn't work, in the sense that when I tried to click on the image to bring up fullscreen in the simulator, nothing happened. I have User Interaction Enabled. I have posted my full code Here

Thanks for your help.

图一

图二

图三

I grabbed your code from your pastebin, and everything works fine. Tap on a small UIImageView, and it shows "full screen" ... tap that image, and it goes away.

Edit : re-reading your comments...

(b) I tried to connect it via the storyboard by selecting the imageview and trying to connect it to the IBAction but it does not allow to connect an outlet

I think that's your mistake. You need to connect the Tap Gesture Recognizer to the IBAction - not the imageview.

Make sure when you have "Art Image" selected, this is what you see:

在此输入图像描述

and, when you have the Tap Gesture Recognizer selected, this is what you see:

在此输入图像描述

I kinda hope it's not this simple, but... double-check the Interaction Enabled checkbox as shown here:

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM