Swift - 如何在单击时全屏显示图像,再次单击时可以创建原始尺寸?

时间:2022-11-04 22:38:47

For the app that I am making I want the user to be able to click an image to make it full screen on the app. And then the user to be able to click the now full screen image to make it the original size.

对于我正在制作的应用程序,我希望用户能够单击图像以使其在应用程序上全屏显示。然后用户可以单击现在的全屏图像使其成为原始大小。

Is this possible?

这可能吗?

Any help would be great, I am just a beginner learner on xcode and am interested in knowing how to do this.

任何帮助都会很棒,我只是xcode的初学者,我有兴趣知道如何做到这一点。

1 个解决方案

#1


57  

Here is code which creates a full screen image (with black bars to preserve aspect ratio) when an image is clicked.

这是在单击图像时创建全屏图像(带有黑条以保持纵横比)的代码。

To use this, add this code to your ViewController which holds the image.

要使用此功能,请将此代码添加到保存图像的ViewController中。

Then, for your imageView that you want to expand, check the box for userInteractionEnabled in the Attributes Inspector, and add a TapGestureRecognizer to it and set it call imageTapped.

然后,对于要扩展的imageView,在Attributes Inspector中选中userInteractionEnabled框,然后向其添加TapGestureRecognizer并将其设置为调用imageTapped。

@IBAction func imageTapped(sender: UITapGestureRecognizer) {
    let imageView = sender.view as! UIImageView
    let newImageView = UIImageView(image: imageView.image)
    newImageView.frame = UIScreen.main.bounds
    newImageView.backgroundColor = .blackColor()
    newImageView.contentMode = .ScaleAspectFit
    newImageView.userInteractionEnabled = true
    let tap = UITapGestureRecognizer(target: self, action: "dismissFullscreenImage:")
    newImageView.addGestureRecognizer(tap)
    self.view.addSubview(newImageView)
    self.navigationController?.isNavigationBarHidden = true
    self.tabBarController?.tabBar.isHidden = true
}

func dismissFullscreenImage(sender: UITapGestureRecognizer) {
    self.navigationController?.isNavigationBarHidden = false
    self.tabBarController?.tabBar.isHidden = false
    sender.view?.removeFromSuperview()
}

This code works by creating a new fullscreen image which covers everything else. It has its own TapGestureRecognizer that removes the fullscreen image from its superView (and thus uncovers the original screen).

此代码的工作原理是创建一个覆盖其他所有内容的新全屏图像。它有自己的TapGestureRecognizer,可以从superView中删除全屏图像(从而揭开原始屏幕)。


Update for Swift 3 and 4:

Swift 3和4的更新:

@IBAction func imageTapped(_ sender: UITapGestureRecognizer) {
    let imageView = sender.view as! UIImageView
    let newImageView = UIImageView(image: imageView.image)
    newImageView.frame = UIScreen.main.bounds
    newImageView.backgroundColor = .black
    newImageView.contentMode = .scaleAspectFit
    newImageView.isUserInteractionEnabled = true
    let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage))
    newImageView.addGestureRecognizer(tap)
    self.view.addSubview(newImageView)
    self.navigationController?.isNavigationBarHidden = true
    self.tabBarController?.tabBar.isHidden = true
}

@objc func dismissFullscreenImage(_ sender: UITapGestureRecognizer) {
    self.navigationController?.isNavigationBarHidden = false
    self.tabBarController?.tabBar.isHidden = false
    sender.view?.removeFromSuperview()
}

#1


57  

Here is code which creates a full screen image (with black bars to preserve aspect ratio) when an image is clicked.

这是在单击图像时创建全屏图像(带有黑条以保持纵横比)的代码。

To use this, add this code to your ViewController which holds the image.

要使用此功能,请将此代码添加到保存图像的ViewController中。

Then, for your imageView that you want to expand, check the box for userInteractionEnabled in the Attributes Inspector, and add a TapGestureRecognizer to it and set it call imageTapped.

然后,对于要扩展的imageView,在Attributes Inspector中选中userInteractionEnabled框,然后向其添加TapGestureRecognizer并将其设置为调用imageTapped。

@IBAction func imageTapped(sender: UITapGestureRecognizer) {
    let imageView = sender.view as! UIImageView
    let newImageView = UIImageView(image: imageView.image)
    newImageView.frame = UIScreen.main.bounds
    newImageView.backgroundColor = .blackColor()
    newImageView.contentMode = .ScaleAspectFit
    newImageView.userInteractionEnabled = true
    let tap = UITapGestureRecognizer(target: self, action: "dismissFullscreenImage:")
    newImageView.addGestureRecognizer(tap)
    self.view.addSubview(newImageView)
    self.navigationController?.isNavigationBarHidden = true
    self.tabBarController?.tabBar.isHidden = true
}

func dismissFullscreenImage(sender: UITapGestureRecognizer) {
    self.navigationController?.isNavigationBarHidden = false
    self.tabBarController?.tabBar.isHidden = false
    sender.view?.removeFromSuperview()
}

This code works by creating a new fullscreen image which covers everything else. It has its own TapGestureRecognizer that removes the fullscreen image from its superView (and thus uncovers the original screen).

此代码的工作原理是创建一个覆盖其他所有内容的新全屏图像。它有自己的TapGestureRecognizer,可以从superView中删除全屏图像(从而揭开原始屏幕)。


Update for Swift 3 and 4:

Swift 3和4的更新:

@IBAction func imageTapped(_ sender: UITapGestureRecognizer) {
    let imageView = sender.view as! UIImageView
    let newImageView = UIImageView(image: imageView.image)
    newImageView.frame = UIScreen.main.bounds
    newImageView.backgroundColor = .black
    newImageView.contentMode = .scaleAspectFit
    newImageView.isUserInteractionEnabled = true
    let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage))
    newImageView.addGestureRecognizer(tap)
    self.view.addSubview(newImageView)
    self.navigationController?.isNavigationBarHidden = true
    self.tabBarController?.tabBar.isHidden = true
}

@objc func dismissFullscreenImage(_ sender: UITapGestureRecognizer) {
    self.navigationController?.isNavigationBarHidden = false
    self.tabBarController?.tabBar.isHidden = false
    sender.view?.removeFromSuperview()
}