Simulate Single/Double Tap Swift - 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

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.

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

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
}

Detecting UIButton tap for button inside of tappable view

I have a container view with a button over it which hides and shows the view. Within the shown view, there are N number of mini buttons that have actions.
The problem I'm having is, when I tap on the mini buttons, those targets are ignored and the larger view button is what receives the action.
How do I configure things so that the larger tappable button on the view still works in most places but where the mini buttons exist, those tap actions register as well?
Thanks!
There are two possible solution
First
Change view hierarchy of uibutton (large on top of the stack in
interface builder)
Like
-Largebutton
-minibutton1
-minibutton1
'
'
-minibuttonn
Second one
Use gesture on the conainer view like
let hideViewGesture = UITapGestureRecognizer(target: self, action: "hideView")
containerView.addGestureRecognizer(hideViewGesture)
func hideView() {
containerView.isHidden = true
}
Its not merely possible to get button action working within a button as
1) Adding a button [Large button] on ContainerView will cause always to detect Large button action and will not allow you to detect button inside it
2) If seen in case of layers large button layer is on top So Logically always large button will first come in Action not inside View of containerView
Possible Solutions :
1) try to make use of gestures on ContainerView
2) you can use a segmented control as show and hide or a UIButton that is placed on side of containerView not over it So you will be able to perform all the required Actions
This is an old question but there actually is a simple solution, by overriding hitTest(_ point: CGPoint, with event: UIEvent?).
Inside your outer button:
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
if self.innerButton.frame.contains(point) {
return self.innerButton
}
else if self.bounds.contains(point) {
return self
}
else {
return super.hitTest(point, with: event)
}
}
It tests to see if the touch point is within the inner button (or you could have several inner buttons), if so it returns it and the touch is registered only by the inner button, if not it goes to the outer view. If neither view contains it, it calls super so the touch is handled correctly by other views.

Adding long press gesture recognizer to UITextView

I'm trying to let the user move a text view around the screen as he presses down on it. The issue I'm getting is my gesture recognizer isn't getting called and instead the text is being selected when I press down on it. How can I disable this? I can't just disable user interaction because a tap should still let the user edit the text. My code is below
let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "longPressed:")
text.addGestureRecognizer(longPressRecognizer)
func longPressed(sender : UITextView) {
println("long press")
}
Try adding this tiny piece of code. Sometimes it makes magic happen ;)
text.userInteractionEnabled = true