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
}
Related
I have created a UI Image view. Is it possible for the user to click on the UI Image view and have the link pop up? If so how would I go about incorporating that? I was thinking possibly through my View Controller using Touches Began and then specifically with a tag, but I'm using Swift's Game View Controller so the Touches Began function is not working properly and won't allow me to convert my SKNODE to Game View Controller type. Any suggestions on how you would do this? Thanks
You are going to want to look at UITapGestureRecognizer. Here is an example below.
if let imageView = yourImageView {
let tap = UITapGestureRecognizer(target: self, action: #selector(self.openLink(_:)))
imageView.addGestureRecognizer(tap)
}
Here is a link if you are also wondering how to open a link in safari.
Swift Open Link in Safari
I just used a gesture recognizer and it worked perfectly
Check out this :
let recognizer = UITapGestureRecognizer(target: self, action: #selector(webViewTapped))
recognizer.delegate = self
webView.addGestureRecognizer(recognizer)
and :
#objc func webViewTapped(_ recognizer: UITapGestureRecognizer) {
if let selectedWebView = recognizer.view as? WKWebView {
selectWebView(selectedWebView)
}
}
func selectWebView(_ webView: WKWebView) {
activeWebView = webView
updateUI(for: webView)
}
I am using image views to display users' friends lists. The user should be able to tap on a friend's icon and be taken to another screen. The code I've written works perfectly on the Xcode simulator. However, when running on a device, the app crashes every single time as soon as I tap an icon.
I really am unsure where to even start debugging this, even after tons of google-ing. Any advice/ help is appreciated.
I have read that my specific error potentially has something to do with memory allocation(?) but still unsure where to start/ what to do. When I check my device logs, the exception type shows this: Exception Type: EXC_BAD_ACCESS (SIGSEGV). I've followed tutorials on finding zombies and it did not help. Thank you.
I'm not really sure what code to post, but since you'll probably want to make sure I am setting up the icons correctly: (i've deleted some code to keep it simple as possible; this is running in a loop to create x amount of icons)
let iv = UIImageView()
iv.translatesAutoresizingMaskIntoConstraints = false
iv.image = image!
iv.tag = j
iv.isUserInteractionEnabled = true
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.iconTapped))
iv.isUserInteractionEnabled = true
iv.addGestureRecognizer(tapGestureRecognizer)
iconTapped function:
func iconTapped(gestureRecognizer: UITapGestureRecognizer, _ sender: AnyObject) {
print("TAPPED NUMBER: \(gestureRecognizer.view?.tag)")
tappedIcon = CurrentSixFriendsList[(gestureRecognizer.view?.tag)!]
let nextVC = ConfirmOpponentViewController()
nextVC.chosenOpponent = tappedIcon
navigationController?.pushViewController(nextVC, animated: true)
}
UPDATE:
I've commented out all code on nextVC and all code in iconTapped except for a print statement. The app still crashes on device only when I tap any friend icon.
func iconTapped(gestureRecognizer: UITapGestureRecognizer, _ sender: AnyObject) {
print("tapped")
}
SECOND UPDATE:
so i commented out all the code for rendering the image views and I hardcoded an image view. (just one image view) with a static image from my project. the image view displays and the app crashes even when i tap on this imageView. why is it acting so strange!? i'm going crazy...
I added this code in VDL just to experiment:
let iv = UIImageView()
iv.isUserInteractionEnabled = true
iv.translatesAutoresizingMaskIntoConstraints = false
iv.image = #imageLiteral(resourceName: "settings icon")
view.addSubview(iv)
iv.widthAnchor.constraint(equalToConstant: 100).isActive = true
iv.heightAnchor.constraint(equalToConstant: 100).isActive = true
iv.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
iv.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(iconTapped))
iv.addGestureRecognizer(tapGestureRecognizer)
Finally, found out why it was crashing.
Your tapped function:
func iconTapped(gestureRecognizer: UITapGestureRecognizer, _ sender: AnyObject) {
print("tapped")
}
should be like this:
func iconTapped(gesture: UITapGestureRecognizer) {
// To get the sender's tag, do this:
print(gesture.view.tag)
}
Also adjust the #selector to the right function.
When setting a selector to UITapGestureRecognizer, the recognizer object will call the selector with only the sender, and you can not add another argument to that function.
So, the root cause of the crash is because you are trying to fit a method with one argument (which is what the gesture sends out) into a method with two arguments. This crash was very interesting because it worked all right on the simulator but only crashed in an actual device. I guess it was because the simulator and the device handle functions with extra parameters differently? (simulator ignores it vs. device crashes). If you find anything deeper as to why exactly this was happening, I would love to know.
Most probly your CurrentSixFriendsList doesnot have value of gestureRecognizer.view?.tag)!.But a breakpoint before this line
tappedIcon = CurrentSixFriendsList[(gestureRecognizer.view?.tag)!]
Even then if you dont get an error then install crashlytics.This is a tool which will tell your crash issue.
Replace your tap gesture code & From swift 3.x you need to use #selector
let tapGesture = UITapGestureRecognizer.init(target: self, action: #selector(dismissKeyboard))
self.view.addGestureRecognizer(tapGesture)
#objc func dismissKeyboard(gesture: UITapGestureRecognizer) {
view.endEditing(true)
}
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)
}
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
When I created a button earlier it had the ability to be an "action" IBA function..
How can I create the same thing with a label? so when I click the label my app will direct to a new page. (and how can I make it to direct to a new page? would I have to make a totally new view and in some way link it to that)
Please bare in mind, I'm a totally new to swift and have already come along way with my program,but its been pieces from several tutorials and therefor I might have a hard time understanding your answer the first time.
You can use UITapGestureRecognizer for that and below is simple example for you:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let label = UILabel(frame: CGRectMake(0, 0, 200, 21))
label.center = CGPointMake(160, 284)
label.textAlignment = NSTextAlignment.Center
label.userInteractionEnabled = true
label.text = "I'am a test label"
self.view.addSubview(label)
let gestureRecognizer = UITapGestureRecognizer(target: self, action: "handleTap:")
label.addGestureRecognizer(gestureRecognizer)
}
func handleTap(gestureRecognizer: UIGestureRecognizer) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("next")
self.presentViewController(vc, animated: true, completion: nil)
}
}
Result:
Project sample for more Info.
Swift 3
1- Make sure you've checked the "User Interaction Enabled" in attributes inspector.
2- Code:
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(YourVC.labelTapped))
yourLabel.addGestureRecognizer(gestureRecognizer)
3- Use the func:
func labelTapped() {
print("labelTapped tapped")
}