2016-11-12 85 views
0

我有一個SKScene,其中有幾個滑動手勢識別器,但滑動手勢很少被識別,我不知道爲什麼。這裏是我的代碼:滑動手勢識別器僅在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。你可以給我的任何幫助都會很棒。感謝您考慮這件事。

真誠,
肖恩

回答

0

它是不是沒有工作的代碼示例。這是什麼情況下是顯而易見的。

您可能在另一個視圖中啓動了您想要的視圖之外的滑動操作,並以您打算的視圖結束。

見這種情況下我的回答&樣本項目:UISwipeGestureRecognizer doesn't recognize swipe gesture initiated outside the view

如果您有其他元素「吃」的UITouch ES,比如按鈕或啓用了互動等意見,解釋它。

如果你想調試,嘗試添加一個UITapGestureRecognizer到您的視圖,然後使用

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

您的子視圖,看看它在視圖中的邊界。

+0

盡我所知,我之所以會出現這個問題,是因爲我在同一個視圖中也有一個平移手勢識別器,即使平移是垂直的而且滑動是水平的。我們通過改變設計來解決問題,以便在底部有響應滑動和敲擊的按鈕。這解決了我們的問題。感謝您提供您的意見。 –