0

I have an SKScene that has a couple of swipe gesture recognizers in it but the swipe gesture is rarely recognized and I don't know why. Here is my code:

class MyScene: SKScene
{
weak var member: MySceneDelegate?
var selectedNode = SKSpriteNode()
var backgroundSprite: MovingBackground!
var myEmitter = SKEmitterNode()
var goingUp: Bool = true
var work: Int = 0
var i = 0.0
var decal: SKSpriteNode!
var startYPosition = 690  //460 is halfway
var myTimer: Timer!
var yStart: CGFloat = 0.0
var count = 0
var currentVal = 1
var startTime = Date()
var xAxisLabelDisplayIncrement = 60.0
var interval = 5.0
var intervalCounter = 0.0
var swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(MyScene.swipe(_:)))
var swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(MyScene.swipe(_:)))

override func didMove(to view: SKView)
{
    //self.view?.isUserInteractionEnabled = true
    let gestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(MyScene.handlePanFrom(_:)))
    self.view!.addGestureRecognizer(gestureRecognizer)
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(MyScene.handleTap(_:)))
    self.view!.addGestureRecognizer(tapGestureRecognizer)
    swipeRight.addTarget(self, action: #selector(MyScene.swipe(_:)))
    swipeRight.direction = UISwipeGestureRecognizerDirection.right
    swipeRight.numberOfTouchesRequired = 1
    self.view!.addGestureRecognizer(swipeRight)

    swipeLeft.addTarget(self, action: #selector(MyScene.swipe(_:)))
    swipeLeft.direction = UISwipeGestureRecognizerDirection.left
    swipeLeft.numberOfTouchesRequired = 1
    self.view!.addGestureRecognizer(swipeLeft)
}
func swipe(_ sender: UISwipeGestureRecognizer)
{
    print("swiped")
    member?.swipe(direction: sender.direction)
}
}

Obviously some code has been omitted to keep this post on point and easy to understand. The interesting thing is I am getting the swipe function to call sometimes, but most of the time it does not print "swiped" or call member?.swipe. Any help you could give me would be fantastic. Thanks for your consideration of this matter.

Sincerely,
Sean

Sean Zlatnik
  • 187
  • 2
  • 12

1 Answers1

0

it's not clear without a working code example what the context of this is.

It's possible that you are initiating the swipe in another view outside of the one you intended and ending in the one you did intend.

See my answer & sample project for this scenario: UISwipeGestureRecognizer doesn't recognize swipe gesture initiated outside the view

If you have other elements that "eat" the UITouches, like buttons or other views with interaction enabled, that explains it.

If you want to debug, try adding a UITapGestureRecognizer to your view, then using

    let location = sender.location(ofTouch: 0, in: mysubview)

for your subviews to see if its within the view boundary.

quantumpotato
  • 9,637
  • 14
  • 70
  • 146
  • As best as I can tell, the reason I was having this problem was because I also had a pan gesture recognizer in the same view, even though the panning was vertical and the swiping was horizontal. We fixed the problem by changing the design so that there are buttons at the bottom that respond to swipes and taps. That solved our problem. Thanks for offering your input. – Sean Zlatnik Oct 18 '17 at 05:59