Why is my image not showing up Fullscreen? - swift

I have the following problem. I have about 10 different images which are in a view controller. Now I want the image to switch to fullscreen when they are tapped on. I already set the allow user interaction option to yes, and put in the following code i found on this website:
import UIKit
class ImageViewController: UIViewController {
#IBAction func imageTapped(sender: UITapGestureRecognizer) {
let imageView = sender.view as! UIImageView
let newImageView = UIImageView(image: imageView.image)
newImageView.frame = self.view.frame
newImageView.backgroundColor = .blackColor()
newImageView.contentMode = .ScaleAspectFit
newImageView.userInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: "dismissFullscreenImage:")
newImageView.addGestureRecognizer(tap)
self.view.addSubview(newImageView)
}
func dismissFullscreenImage(sender: UITapGestureRecognizer) {
sender.view?.removeFromSuperview()
}
Does anyone know why my images are not tappable and turning fullscreen?

Make sure both the image view have userInteractionEnabled = YES and
here's an example for what you are trying to do here https://github.com/abhinavsingh77/ImagePreview

Related

How to make an image disappear after clicking it?

I want to make a small image.png disappears after clicking it.
I'm a newborn of swift so I'm using single view on Playground for my experiments. Anyone can help me please?
add this code while creating your_imageView
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
your_imageView.isUserInteractionEnabled = true
your_imageView.addGestureRecognizer(tapGestureRecognizer)
func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
{
let tappedImage = tapGestureRecognizer.view as! UIImageView
tappedImage.isHidden = true
}

How can I change ViewController with tapping an UIImageView?

I created a CollectionView. This view has a userProfileImageView and I want to change view controller to ProfilePage when clicked this user profile image. Try some code but neither present nor push view controller doesn't work to me. Can you help me ?
lazy var userProfileImageView: UIImageView = {
let useriv = UIImageView()
useriv.contentMode = .scaleAspectFill
useriv.clipsToBounds = true
useriv.layer.cornerRadius = 40 / 2
useriv.isUserInteractionEnabled = true
useriv.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(userImageTap)))
return useriv
}()
#objc func userImageTap() {
print("useriv clicked")
let profilePage = ProfilePage(collectionViewLayout: UICollectionViewFlowLayout())
}

My addSubview for an image adds a larger view than expected

I have an image inside a table view cell where the user can user tap on the image the app adds a subview with that image in it. The table view can be longer than the screen. The issue is that if I have the table view scrolled up a bit and the user taps on the image, the image in the subview is scrolled up. My guess is that calling newImageView.frame = UIScreen.main.bounds isn't what I need?
#IBAction func imageTapped(_ sender: UITapGestureRecognizer) {
let imageView = sender.view as! UIImageView
let bookImage = UIImageView(image: imageView.image)
bookImage.frame = UIScreen.main.bounds
bookImage.backgroundColor = .black
bookImage.contentMode = .scaleAspectFit
bookImage.isUserInteractionEnabled = true
let bookImageTap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage))
bookImage.addGestureRecognizer(bookImageTap)
self.view.addSubview(bookImage)
self.navigationController?.isNavigationBarHidden = true
self.tabBarController?.tabBar.isHidden = true
}
this is what it’s suppose to look like wherever I tap it
this is what happens if I scroll the table view up and tap the image
self.view is a table view, which is a kind of scroll view. If you add a subview to that, it will scroll with the table view. I suppose you don't want that.
Therefore, you should not add the bookImage as a subview of self.view. You can instead add it as a subview of the window:
let window = (UIApplication.shared.delegate as! AppDelegate).window
window?.addSubview(bookImage)

UIImage gets clipped when using UiTapGesturerecognizer

I am using the code below to display my image in full screen when a user taps on the image. This image is inside a tableview.
func imageTapped(_ sender: UITapGestureRecognizer) {
let imageView = self.moreImageView
let newImageView = UIImageView(image: imageView?.image)
newImageView.frame = UIScreen.main.bounds
newImageView.backgroundColor = .black
//newImageView.sizeToFit()
newImageView.contentMode = .scaleAspectFill
newImageView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage))
newImageView.addGestureRecognizer(tap)
self.addSubview(newImageView)
}
But when I tap the screen the image renders full screen, but the upper and lower parts of the image always gets clipped. How do I make sure the image resizes and fits in correctly? Thanks
You want .scaleAspectFit
Fit will make it fit inside the space (and show the background color on parts that don't fill)

Display menu list on Double Tap gesture - Swift3

I'm new to IOS and I'm trying to display list of buttons on double tap of UIView. I have the below code to capture the double tap gesture:
let menuTap = UITapGestureRecognizer(target: self, action: #selector(showMenuPanel(_:)))
menuTap.minimumPressDuration = 0.0
menuTap.numberOfTapsRequired = 2
menuTap.delaysTouchesBegan = true
self.view.isUserInteractionEnabled = true
self.view!.addGestureRecognizer(menuTap)
func showMenuPanel(_ recognizer: UITapGestureRecognizer) {
print("TESTPANEL")
}
I need to design a panel to show a list of buttons such as start, stop and pause. Can anybody guide me on how to design a panel on the tapped position?
You need to use UITapGestureRecognizer for that not the UILongPressGestureRecognizer. Set UITapGestureRecognizer with numberOfTapsRequired to 2.
let menuTap = UITapGestureRecognizer(target: self, action: #selector(showMenuPanel(_:)))
menuTap.numberOfTapsRequired = 2
//No need to set isUserInteractionEnabled to true because by default it is true for `UIView`
//self.view.isUserInteractionEnabled = true
self.view!.addGestureRecognizer(menuTap)
Add action method of tapGesture like this.
func showMenuPanel(_ recognizer: UITapGestureRecognizer) {
print("TESTPANEL")
let point = recognizer.location(in: self.view)
//Get your view from nib
let view = CustomView()
//set its origin to this point
view.frame.origin = point
//add your view in self.view
self.view.addSubview(view)
}