How can I find button tag after implementing gesture recogniser in swift? - 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
}
}

Related

How can I load a website by clicking a UIImage View?

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)
}

How to add a UILongPressGestureRecognizer release function

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.

Swift: Segue to another view when Map View is tapped

I would like to perform a segue to another view once a map view is tapped like when you check a location and tap on map on instagram, or like in foursquare.
I could not connect map view itself to another view directly like we do in buttons, and I don't want to perform segue when callout button is tapped.
I found a workaround by placing an invisible button into mapView, and performing segue when the button is tapped(user thinks mapView is tapped)
But I would like to know if there is a better solution for it, without a button.
You can use UITapGestureRecognizer
let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.tapHandler(_:)))
mapView.addGestureRecognizer(gestureRecognizer)
#objc func tapHandler(_ gestureReconizer: UIGestureRecognizer) {
// let location = gestureReconizer.location(in: mapView)
// coordinatte of taplocation
// let coordinate = mapView.convert(location,toCoordinateFrom: mapView)
// **you can navigate or perform Your Actions there**
}

UILongPressGestureRecognizer and UITapGestureRecognizer

I am quite new to Swift and I am fascinated to the potential of distinguish these two gesture for a button.
I am writing my first app in xCode and I am near to conclude that. As a last step I want to implement two different actions for a button depending on a long press or a tap.
I have constructed the app as follows. I have several buttons connected to one IBAction and distinguished them using tags.
coming to the tag of the one of the two buttons on which I need the long press action I don't know how to continue.
Do you have some suggestion?
Thank you so much
func longTap() {
if (resultDisplay.text != ""){
storedVariableA = String(result)
eraseAll()
}
}
else if (sender.tag == 20) {
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.longTap(_:)))
longPressGesture.minimumPressDuration = 2
sender.addGestureRecognizer(longPressGesture)
}
You can check in your #IBAction for the tag you given in storyboard or programmatically, Please check the below code.
#IBAction func action(_ sender: UIButton) {
if sender.tag == 22 { // check for your desired tag instead of "22"
// add longpress gesture. on sender // sender represents your button.
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.handleLongPressGesture(_:)))
longPressGesture.minimumPressDuration = 2 // mention minimum press duration you want user to press.
sender.addGestureRecognizer(longPressGesture)
} else {
}
}

swift swreveal - showing toggling to side menu using button or tap gesture

I'm trying to get my side menu (swreveal) to show when clicking a UIImageView with a tap gesture or button. I've got the image view and tap all set up and the tap gesture linked to an action. Getting this functionality is easy when using a bar button item but I can't seem to get it to function when using a plain old button or image view.
Here's how I do it for a bar button item:
barButtonItem!.target = self.revealViewController()
barButtonItem!.action = Selector("revealToggle:")
This obviously doesn't work for buttons or ImageViews because they don't have a member of target or action. Would really appreciate some assistance on this one!
To add target to UIButton you can use this:
...
let button = UIButton()
button.addTarget(self, action: "someAction:", forControlEvents: .TouchUpInside)
...
func someAction(sender: UIButton!) {
...
}
For adding action to press on UIImageView you can use UITapGestureRecognizer like this:
...
let tap = UITapGestureRecognizer(target: self, action: Selector("someAction"))
tap.numberOfTapsRequired = 1
image.userInteractionEnabled = true
image.addGestureRecognizer(tap)
...
func someAction() {
...
}