I'm using Mapbox iOS SDK, making the next layout: mapView and two buttons ("zoom in" and "zoom out").
I can't figure out how to correctly handle double tap on buttons.
Double tap on "zoom out" button zooms me in - as far as I understand, Mapbox's mapView intercept and handle my double tap.
I've tried the following code in viewDidLoad:
let doubleTap = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap))
doubleTap.numberOfTapsRequired = 2
zoomOutButton.addGestureRecognizer(doubleTap)
let singleTap = UITapGestureRecognizer(target: self, action: #selector(handleSingleTap))
singleTap.numberOfTapsRequired = 1
singleTap.require(toFail: doubleTap)
zoomOutButton.addGestureRecognizer(singleTap)
and handlers (just to see it works):
func handleSingleTap(tap: UITapGestureRecognizer) {
print("single")
}
func handleDoubleTap(tap: UITapGestureRecognizer) {
print("double")
}
It doesn't work. How can I handle double tap on "zoom out" button to zoom out twice?
Related
I'm working on a small SpriteKit game for tvOS. I need to receive input when the user clicks the play/pause button on the remote. Looking at the docs, it appears that I should just have to add a UITapGestureRecognizer to my scene's view. I implemented the following code:
override func didMove(to view: SKView) {
let tapPlayPause = UITapGestureRecognizer(target: self, action: #selector(tapTesting))
tapPlayPause.allowedPressTypes = [NSNumber(value: UIPress.PressType.playPause.rawValue)]
view.addGestureRecognizer(tapPlayPause)
let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(swipeUp))
swipeUp.direction = .up
view.addGestureRecognizer(swipeUp)
}
#objc func tapTesting(_ sender: UITapGestureRecognizer) {
print("TAP")
}
However, when I run the app on my Apple TV 4K (running tvOS 15), there is no output and from what I can tell no tap code is getting triggered.
I have tried the code with self.isUserInteractionEnabled = true as well but I still do not receive tap inputs.
The swipe gesture I implemented above works perfectly thought, and the tap gesture works on my iOS devices. Does anyone have any idea as to why this is the case?
Thanks!
For tvOS gestures, you also need to set allowedTouchTypes to .indirect
tapPlayPause.allowedTouchTypes = [NSNumber(touchType: .indirect)]
I have tested the following code, and only the selectors for the pan, tap, and long press recognizers activate. I have tried adding the recognizers inside the UIView subclass and adding the recognizers as parameters. My only guess would be that the pan, tap, and long press are somehow gobbling up all of the touches, but I would expect only one of them to work in that case. Since the same procedures are followed for all recognizers, I don't really understand what the problem would be. Thanks in advance for any help.
init(level: LevelView) {
self.level = level
left = UISwipeGestureRecognizer(target: self, action: #selector(swipeLeft))
right = UISwipeGestureRecognizer(target: self, action: #selector(swipeRight))
up = UISwipeGestureRecognizer(target: self, action: #selector(swipeUp))
down = UISwipeGestureRecognizer(target: self, action: #selector(swipeDown))
tap = UITapGestureRecognizer(target: self, action: #selector(tap(_:)))
pan = UIPanGestureRecognizer(target: self, action: #selector(pan(_:)))
long = UILongPressGestureRecognizer(target: self, action: #selector(long(_: )))
long.minimumPressDuration = 0.3
long.allowableMovement = 30
left.direction = .left
right.direction = .right
up.direction = .up
down.direction = .down
for rec in [up, down, right, left, tap, long, pan] {
rec!.cancelsTouchesInView = false
level.addGestureRecognizer(rec!)
}
}
Update:
The Pan recognizer was immediately recognizing the touches and not passing them to the swipe recognizers. Since I wanted the swipe recognizers to take precedence, I added the condition that all of the swipe gestures fail before the pan recognizer could recognize. That worked. Thank you to Matt for the helpful tip.
I'm working with ArcGis Map in my ios app. I'm trying to apply a long press gesture on it. But i'm getting an error Value of type 'BCBaseMapView?' has no member 'addGestureRecognizer' . How i can add long gesture on it. This is what i coded in it.
let lpgr = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress))
lpgr.minimumPressDuration = 0.5
lpgr.delaysTouchesBegan = true
// lpgr.delegate = self
self.mapView.addGestureRecognizer(lpgr)
#objc func handleLongPress(sender: UILongPressGestureRecognizer)
{
print("longpressed")
self.addWaypointOnMap()
}
This is my code i'm getting error on this line.
self.mapView.addGestureRecognizer(lpgr)
This is my mapView
var mapView: BCBaseMapView?
If the superview of BCBaseMapView is MKMapView then try to add it on super view using:
self.mapView.super.addGestureRecognizer(lpgr)
OR
self.mapView.superview.addGestureRecognizer(lpgr)
I want to start pan gesture in 1 item. And at the instant the gesture is recognized I create a new item view at the exact same spot and transfer the pan gesture to the new item so the new hovering item moves with the fingers.
How can I do this?
Here is the code so far:
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(useItemPanGesture(_:)))
item.addGestureRecognizer(panGesture)
func useItemPanGesture(panGesture: UIPanGestureRecognizer) {
let newView = createView()
newView.addPanGesture(target: self, action: #selector(self.actualPanGesture(panGesture)))
panGesture.enabled = false
}
func actualPanGesture(panGesture: UIPanGestureRecognizer) {
//do pan gesture stuff
}
I have trouble in the transfer of the pan gesture. Line starting with newView.addPangesture.
I'm trying to create a keyboard the allows single tap, double tap, and triple tap. So I want to add a UITapGestureRecognizer() to each button in my keyboard. I know how to do this manually from the xib file (add each letter it's own gesture, which would take ages) but not quite sure how to do it in the Controller.
I wrote this for double tap in the viewDidLoad() method:
let doubleTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "doubleTapCharacter:")
doubleTap.numberOfTapsRequired = 2
for button in self.view.subviews{
button.addGestureRecognizer(doubleTap)
}
and created a doubleTapCharacter() method but it's still not working. I also want to be able to send information to the doubleTapCharacter method.
Any help would be much appreciated. Also, I'm very new to swift so if the instructions are complicated, I'd highly appreciate it if you can break it down a little.
create and add the gesture recognizers:
for button in view.subviews {
// create the gesture recognizer
let doubleTapRecognizer = UITapGestureRecognizer(target: self, action: "doubleTapCharacter:")
doubleTapRecognizer.numberOfTapsRequired = 2
// add gesture recognizer to button
button.addGestureRecognizer(doubleTapRecognizer)
}
then implement the target method:
func doubleTapCharacter(doubleTapRecognizer: UITapGestureRecognizer) {
let tappedButton = doubleTapRecognizer.view as! UIButton
print(tappedButton.titleForState(UIControlState.Normal))
}