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)]
Related
In my project I have tap gesture with following setup
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(sceneViewTapped(gesture:)))
tapGesture.cancelsTouchesInView = true
tapGesture.delaysTouchesBegan = true
self.view.addGestureRecognizer(tapGesture)
And also has Touch method like override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
I have setup touch gesture with tapGesture.delaysTouchesBegan = true to work both gesture as well as Touch delegate methods
It is working fine till iOS12, But in iOS13 Touch delay has been increased so user must drag his finger and wait until drawing starts because touch method called delayed
Please refer below image , Sometime gesture lost , Delayed on starting of drawing.
If I remove delaysTouchesBegan it is smooth again.
Can anyone can help me to solve this ?
SAMPLE PROJECT
To quick test this stuff download raywenderlich project https://www.raywenderlich.com/5895-uikit-drawing-tutorial-how-to-make-a-simple-drawing-app
Add Following code in ViewController.swlft
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(viewTapped(gesture:)))
tapGesture.delaysTouchesBegan = true
self.view.addGestureRecognizer(tapGesture)
}
#objc func viewTapped(gesture:UITapGestureRecognizer) {
print("View Tapped")
}
This was iOS 13.0 bug
After update to the iOS 13.1 Will fix this issue automatically
Hopefully helpful to someone :)
I am adding a UITapGesture to some views. It is working fine only in iPhone 7 plus. The gesture method does not get called for other devices(iPhone 8, 6, 5s). Has anyone faced this kind of issue? I am just curious why is this happening? Has this something to do with Autolayout?
let tabGesture = UITapGestureRecognizer(target: self, action: #selector(handlePassTap(_:)))
tabGesture.numberOfTapsRequired = 1
self.passTabContainer.addGestureRecognizer(tabGesture)
let textGesture = UITapGestureRecognizer(target: self, action: #selector(handlePassTap(_:)))
textGesture.numberOfTapsRequired = 1
self.passTextContainer.addGestureRecognizer(textGesture)
#objc func handlePassTap(_ gesture:UITapGestureRecognizer)
{
performSegue(withIdentifier: "passSegue", sender: currentPass)
}
Yes. There was a view on top of those gesture views that was blocking the tap. Resolved it now.
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?
override func viewDidAppear(animated: Bool) {
view.userInteractionEnabled = true
let pinchGesture:UIPinchGestureRecognizer = UIPinchGestureRecognizer(target: self, action: "pinchGesture")
view.addGestureRecognizer(pinchGesture)
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
view.addGestureRecognizer(tap)
}
func dismissKeyboard() {
view.endEditing(true)
}
func pinchGesture(){
self.performSegueWithIdentifier("trick1Segue", sender: self)
}
In my iOS app, i want to transition to a different view controller when a pinch gesture is performed on the screen.
The tap gesture is to dismiss the keyboard when tapped outside the keyboard area
While running the app, I get an error message:
"Attempting to present < ** > on < ** > while a presentation is in progress"
The new view controller appears but opens twice, with a very short time difference. Looked up a lot of blogs but couldn't find a solution, please help!!!
The problem is that pinchGesture can be called multiple times. You should add a property to your viewController to keep track of the fact that you already have acted upon the pinch gesture:
var segueInProcess = false
func pinchGesture() {
if !segueInProcess {
self.performSegueWithIdentifier("trick1Segue", sender: self)
segueInProcess = true
}
}
Gesture recognizers are called multiple times with different states. What you can do is check the state property of the UIPinchGestureRecognizer in pinchGesture().
I'm building a iOS Swift (Sprite kit) game. I'm using swipes to move the player. While testing I've found out that everything works just fine on most devices. However, a few devices do not recognise the swipe. Needless to say, the game doesn't work without the swipes. It's very odd that it works on almost all devices but a few.
My code is below. (Note: It's cleaned up to show only the code relevant for this case)
Edit
The app is currently being tested on actual devices. All devices run the latest iOS software. Mostly iPhone 5, a few 6 and 5S. Error occurs on iPhone 5S.
Am I missing something? If not, what else could I try?
override func didMoveToView(view: SKView) {
addSwipes()
}
func addSwipes() {
let swipeRight:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedRight:"))
swipeRight.direction = .Right
view!.addGestureRecognizer(swipeRight)
let swipeLeft:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedLeft:"))
swipeLeft.direction = .Left
view!.addGestureRecognizer(swipeLeft)
}
func swipedRight(sender:UISwipeGestureRecognizer) {
// Do code here
}
func swipedLeft(sender:UISwipeGestureRecognizer) {
// Do code here
}