Swipe Gesture Recognizer在SKScene中间歇性地工作

时间:2016-11-12 07:11:28

标签: ios swift skscene uiswipegesturerecognizer

我有一个SKScene,其中有几个滑动手势识别器,但滑动手势很少被识别,我不知道为什么。这是我的代码:

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

显然,省略了一些代码以保持这篇文章的简单易懂。有趣的是我有时会调用滑动功能,但大多数情况下它不打印“刷”或呼叫成员?.swipe。你能给我的任何帮助都会很棒。感谢您对此事的考虑。

此致
肖恩

1 个答案:

答案 0 :(得分:0)

如果没有一个正常工作的代码示例,它就不清楚这是什么情况。

您可能会在您想要的视图之外的另一个视图中启动滑动,并以您想要的视图结束。

请参阅我的回答&此业务情景的示例项目:UISwipeGestureRecognizer doesn't recognize swipe gesture initiated outside the view

如果你有其他元素" eat" UITouch es,如启用了交互的按钮或其他视图,可以解释它。

如果您想进行调试,请尝试在视图中添加UITapGestureRecognizer,然后使用

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

为您的子视图查看它是否在视图边界内。