How to add a UILongPressGestureRecognizer release function - swift

I am using a long press gesture recognizer because without it if I click and release quickly on the buttons, the code doesn't execute properly. But with the long press gesture recognizer, my buttonUp function does not execute. How can I check if a finger is off the screen using long press gesture recognizer?

You may refer to this if you want to have release action and hold down action in your button!
OR
You can check on the state of gesture in your long press here!
OR
Handling a long-press gesture from Apple Developer Documentation
Hope it helps.
Cheers.

If you want to perform any action with a single tap and long press you can add gestures into button this way:
#IBOutlet weak var btn: UIButton!
override func viewDidLoad() {
let tapGesture = UITapGestureRecognizer(target: self, #selector (tap)) //Tap function will call when user tap on button
let longGesture = UILongPressGestureRecognizer(target: self, #selector(long)) //Long function will call when user long press on button.
tapGesture.numberOfTapsRequired = 1
btn.addGestureRecognizer(tapGesture)
btn.addGestureRecognizer(longGesture)
}
#objc func tap() {
print("Tap happend")
}
#objc func long() {
print("Long press")
}
This way you can add multiple methods for a single button and you just need Outlet for that button for that.

Related

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 call a function when the Compass Button of a mapView is pressed

I have developed an app showing a map view with a compass button. The natural function when pressing this compass button is that the map is being oriented to north.
Now I want to call a function when pressing this compass button.
A view of the app with a map can be found there (compass button is visible in the upper right corner)
http://aceingolf.com/wp-content/uploads/2018/12/0.jpg
The code below is working fine. It is just shown to give a complete view.
#IBOutlet weak var mapView: MKMapView!
let compassButton = MKCompassButton(mapView: mapView)
compassButton.compassVisibility = .visible
mapView.addSubview(compassButton)
I am looking for a way to create an action when the user presses the compass button on the map view.
I believe MKCompassButton inherits from UIView, So you can just add a UITapGestureRecognizer to the compassButton with an action like following:
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleCompassTap))
compassButton.addGestureRecognizer(tapGestureRecognizer)
#objc func handleCompassTap() {
// Implement your feature when the compass button tapped.
}
Take emrepuns code
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleCompassTap))
compassButton.addGestureRecognizer(tapGestureRecognizer)
#objc func handleCompassTap() {
// Implement your feature when the compass button tapped.
}
and add this line
tapGestureRecognizer.numberOfTouchesRequired = 1
or
tapGestureRecognizer.numberOfTapsRequired = 1

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

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

How can I find button tag after implementing gesture recogniser in swift?

I want an action to be done once when someone swipes within a button.
My current code is as follows:
let recogniser = UISwipeGestureRecognizer(target: self, action: "didTapButton2:")
recogniser.direction = .Up
button.addGestureRecognizer(recogniser)
func didTapButton2(sender: UIGestureRecognizer!) {
//Here I want to be able to recognise which button this was sent from (they are all tagged)
let button = sender. as UIButton //Gives an error
I need to use gesture recognisers as opposed to UIControlEvents as I need the event to only fire once. Using this makes the event fire loads of times - just need it to go once:
button.addTarget(self, action: "didTapButton2:", forControlEvents: .TouchDragInside)
Does anyone have a solution? Thanks
A UIGestureRecognizer has a property called view which is the view that it is attached to. View is declared as an optional, so you need to unwrap it:
if let button = sender.view as? UIButton {
// use button
if button.tag == 10 {
// handle button tagged with 10
}
}