当前位置:  开发笔记 > IOS > 正文

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

如何解决《Swift-如何在单击时全屏显示图像,再次单击时创建原始尺寸?》经验,为你挑选了1个好方法。

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

这可能吗?

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



1> vacawama..:

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

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

然后,对于要扩展的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()
}

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


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()
}


对于那些希望复制/粘贴的人:在`viewDidLoad`中添加`let pictureTap = UITapGestureRecognizer(target:self,action:#selector(YourClass.yourFunction))``然后`yourImageView.addGestureRecognizer(pictureTap)```yourImageView.isUserInteractionEnabled = true //也可以通过storyboard`设置
推荐阅读
周扒pi
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有