How to call a function when the Compass Button of a mapView is pressed - swift

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

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

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() {
...
}