Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - How can I make an image full screen when clicked and then original size when clicked again? [closed]

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.

like image 499
Tessa Avatar asked Jan 09 '16 14:01

Tessa


People also ask

How do I fill an image with Xcode?

Click on the Image View you added to your View Controller. In the Attributes Inspector on the right, choose your image. Set the Mode of the View to Aspect Fill (fill entire screen cropping top/bottom or left/right as necessary) or Aspect Fit (letter box image so that entire image is shown uncropped).

How do I change the width and height of an image in Swift?

Show activity on this post. extension UIImage { func imageResize (sizeChange:CGSize)-> UIImage{ let hasAlpha = true let scale: CGFloat = 0.0 // Use scale factor of main screen UIGraphicsBeginImageContextWithOptions(sizeChange, ! hasAlpha, scale) self. draw(in: CGRect(origin: CGPoint.

How do I click an image in Swift?

This is what I found, much easier to setup. Open the Library, look for "Tap Gesture Recognizer" object. Drag the object to your storyboard, and set the delegate to the image you want to trigger actions. Then go to the view controller, drag the same object to set the IBAction.


1 Answers

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.

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.

@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).


Update for Swift 3 and 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() } 
like image 103
vacawama Avatar answered Sep 20 '22 14:09

vacawama