当用户在Swift中点击我的UIImage时,如何在全屏显示图像?

我在我的应用程序中显示一张照片,我正在使用UIImage 。 到目前为止,这是我如何做到这一点:

 func getDataFromUrl(url:NSURL, completion: ((data: NSData?, response: NSURLResponse?, error: NSError? ) -> Void)) { NSURLSession.sharedSession().dataTaskWithURL(url) { (data, response, error) in completion(data: data, response: response, error: error) }.resume() } func downloadImage(url: NSURL){ getDataFromUrl(url) { (data, response, error) in dispatch_async(dispatch_get_main_queue()) { () -> Void in guard let data = data where error == nil else { return } print("Download Finished") self.requestPhoto.image = UIImage(data: data) } } } 

然后在viewDidLoad()我在做:

 if let checkedUrl = NSURL(string: photo) { requestPhoto.contentMode = .ScaleAspectFit downloadImage(checkedUrl) } 

这留下了UIImage充满了我的照片,但它不是可点击的,这是原始组件的大小。 有没有办法添加某种监听器或什么东西,当用户点击UIImage组件时将全屏显示照片?

你需要的是添加一个UITapGestureRecognizer到你的requestPhoto图像视图。 像这样的东西:

 let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector("tapHandler")) self.requestPhoto.addGestureRecognizer(tapGestureRecognizer) self.requestPhoto.userInteractionEnabled = true 

最后一行是需要的,因为UIImageView已经默认打开了,所以触摸被取消。

然后在同一个class上:

 func tapHandler(sender: UITapGestureRecognizer) { if sender.state == .Ended { // change the size of the image view so that it fills the whole screen } } 

整个事情也可以从一个标志说,如果视图是全屏或不,所以当用户点击全屏图像,你可以扭转过程。

与往常一样,您可以在文档中阅读更多内容。

如何将缩略图照片放大到iOS上的全屏照片视图?

我通过创build具有相同图像的新的UIImageView对象来解决这个问题,并将其框架设置为缩略图:

 let fullscreenPhoto = UIImageView(frame: thumbnailImage.frame) 

然后将图像作为子图层添加到主窗口中:

 UIApplication.sharedApplication().windows.first!.addSubview(fullscreenPhoto) 

使用块的animation进行大小调整:

 let windowFrame = UIApplication.sharedApplication().windows.first!.frame UIView.animateWithDuration(0.4, delay: 0.0, options: .CurveEaseInOut, animations: { fullscreenPhoto.frame = windowFrame fullscreenPhoto.alpha = 1 fullscreenPhoto.layoutSubviews() }, completion: { _ in }) 

你可以在这里find现成的解决scheme: https : //github.com/lukszar/SLImageView

提供用于显示和隐藏全屏的手势识别器。 您只需要在Storyboard中将您的UIImageView设置为SLImageView类,并完成所有的魔术。