Need to do double click in a button to change the image - swift

Swift
I am new in swift and I am doing a tic tac toe, with some improvements in the gaming, so I need to put some condicionals, but my real problem is when I am playing I need to do double click to change a button with no image to a button with image and this is anoying to the player.
I put somes Outlet because I need to desable some buttons (this depend of the players) but I think this no has relation with my mistake because one day I errased all my Outlets to found my mistake and I still needed to do double click, thanks for helping and have a nice day.

You can specify the number of taps required for a UITapGestureRecognizer. Instead of the view, you could set to the button/image.
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(doubleTapped))
tap.numberOfTapsRequired = 2
view.addGestureRecognizer(tap)
}
#objc func doubleTapped() {
// do something here
}

Related

How to add Swipe Gesture on a view that has a Pan Gesture as well?

So I have a view with a carousel that has two views in it. I have two buttons that navigate from the two views inside the carousel. When the user is on the right view, the right button is disabled and left is enabled. When the user is on the left view, the left button is disabled and right is enabled.
However, I have a Pan gesture that allows the user to also swipe in between the two pages without using the buttons.
When I use the gesture to move in between the two views, I also need the buttons to reflect the change (either be enabled or disabled), however, when I put it on my pan gesture function it doesn't work 100% of the time as a half swipe or short swipe not strong enough to shift the pages but is strong enough to disable/enable my buttons as if the view has changed even though it hasn't.
I tried incorporating a Handle Swipe but I couldn't get the function to work.
Can anyone take a look at this code and let me know if anything is missing? Or better yet, if there is an easier solution to accomplish my goal
#objc func yesswiped (recognizer: UISwipeGestureRecognizer){
print("swipe pls")
}
override func viewDidLoad() {
super.viewDidLoad()
setNavigationBar()
let leftSwipe = UISwipeGestureRecognizer(target: self, action: #selector(yesswiped))
let rightSwipe = UISwipeGestureRecognizer(target: self, action: #selector(yesswiped))
leftSwipe.direction = .left
rightSwipe.direction = .right
self.view.addGestureRecognizer(leftSwipe)
self.view.addGestureRecognizer(rightSwipe)
}

How can I have a button and scroll view in the same spot?

To build context, my app has a stack of cards (similar to Tinder) that flip and each could contain a lot of text on one side that require a UITextView for scrolling. It's basically a flashcard app, so once the user is done looking at the card, they swipe it away to view the next one.
I'm trying to make it so the user can tap anywhere on the card to flip it, so I added a clear button that fills up the card. My problem is that when the button is at the front, it blocks the text view from being scrollable, and when the button is at the back, the text view blocks it from being pressed.
Is there a way around this somehow to allow for both functionalities?
You can create a UITapGestureRecognizer, like so:
let gesture = UITapGestureRecognizer(target: self, action: #selector(tap(gesture:)))
addGestureRecognizer(gesture)
And the function to trigger:
#objc func tap(gesture: UITapGestureRecognizer) {
print("Tap!")
}
Maybe you can add a UITapGestureRecognizer to the text view itself. Something like:
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.didTap(_:)))
textView.addGestureRecognizer(tapGesture)
#objc func didTap(_ gesture: UITapGestureRecognizer) {
// Handle the tap
}
did you try a tableView or a collectionView, you can customize your cell to look like a card then when a user clicks the cell it will flip or present a popup view, thats easier.

How to print "image x tapped" when user taps on one of many UIImages in the View Controller? (Swift 3)

I have a set of 12 UIImageViews in my storyboard, and, for argument's sake, I want to get each one to print to logs "You just tapped image x", when the user taps on it, where x is the number of image tapped, from 1-12). So i need to detect which image is tapped, and do something depending on that information. What would be the best way to do this, in Swift 3 ?
(I assume 12 IBActions -treat them as button with an image on background- is really bad code. Also they need to be placed on specific positions on top of a background image, so cannot use UICollectionView to do this.) Thanks
First of all, I think using a collectionView is a better approach to achieve what do you want. However, you'll need to:
Set userInteractionEnabled to true for all of your imageViews.
Set -sequential- tag for all of your imageViews, for example image1.tag = 1, image2.tag = 2 ... and so on.
Implement a method to be the target of all of your images tapping, it should be similar to this:
func imageViewTapped(imageView: UIImageView) {
print("You just tapped image (imageView.tag)")
}
Create -one single- tap gesture and assign the implemented method to its selector:
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(imageViewTapped))
Finally, add the tapGesture for all of your image, for example: image1.addGestureRecognizer(tapGesture), image2.addGestureRecognizer(tapGesture)... and so on.
Hope this helps.
It's not bad code per-se, but if you did it that way, or with tap gestures on each of the ImageViews you would likely want to separate out that part of the logic.
There are other approaches/views you could use to manage this kind of thing better, especially if this is supposed to scale, but with your constraints this is what I would suggest:
Either add TapGestureRecognizers to your imageViews or make them buttons, then connect all their actions to this:
#IBAction func phoneWasPressed(sender: AnyObject) {
guard let tappedImageView = sender as? UIImageView else {
return
}
switch tappedImageView {
case imageView1:
//do something
case imageView2:
// do something else
//etc.
default:
break
}
switch sender
}
I don't think it's a bad idea at all to have 12 buttons. Assign each of them a tag from 1-12 and connect them to the same IBAction.
#IBAction func didTapImageButton(button: UIButton) {
print("You just tapped image \(button.tag)")
}

Simulate Single/Double Tap Swift

Is it possible to simulate a single/double tap through Swift? I am using the Charts library from GitHub, and it has the ability to highlight an area when selected (finger pressed), but when you remove your finger, the area stays selected. I'd like to add the ability that when the finger selection is done, to simulate a single tap on the same place, or a double tap elsewhere on the graph, as that de-selects the area. Thanks!
You have to add an UITapGestureRecognizer, for example:
let tap = UITapGestureRecognizer(target: self, action: #selector(UIViewController.handleTap(sender:)))
chartView.addGestureRecognizer(tap)
Then you have to implement the handler:
#objc func handleTap(sender: UITapGestureRecognizer? = nil) {
// call the touch chart event here
}
After that, to simulate the tap, you just call the handleTap() function

Keyboard issue on my app

I just made a simple app where you type things in four boxes and can randomly select one. After typing in the boxes I need the keyboard to go away so people can see what the result is, but the on-screen keyboard just stays. Is this something I need to change in the files of the app?
You have to use resignFirstResponder() property of the text field.
If you want to hide the keyboard when a tap occurred outside the text field,
you can use an UITapGestureRecognizer.
override func viewDidLoad() {
super.viewDidLoad()
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(backgroundTapped(_:)))
view.addGestureRecognizer(tapRecognizer)
}
func backgroundTapped(sender: UITapGestureRecognizer) {
view.endEditing(true)
}