2015-09-07 98 views
0

我想滾動精靈套件節點沒有UIScrollView的滾動SpriteNode使用觸摸

// 在touchesMoved

let touch: UITouch = touches.first as! UITouch; 
let location:CGPoint = touch.locationInNode(self); 
let lastPosition = touch.previousLocationInNode(self) 

var newLoc = selectNode.position.y + (location.y - lastPosition.y) 
// Check if the top part is reached 
if(newLoc < selectNodeStart){ 
    newLoc = selectNodeStart 
} 

if(selectNode.position.y >= selectNodeStart){ 
    // let sk = SKAction.moveToY(newLoc, duration: 0.1) 
    // selectNode.runAction(sk) 
    selectNode.position.y = newLoc 
} 

如果我使用SK行動的結果是非常可怕的,但沒有結果也是可怕的

回答

1

在SpriteKit中這樣做的一種優雅的方式是使用UIPanGestureRecognizer來檢測觸摸,並在其中你無線我們將創建一系列可以加速或減速運動的SKA。您可能必須使用update方法和/或touches來處理平底鍋停止或另一個立即開始。不幸的是,如果你只想使用SpriteKit,你將不得不使用SKActions來實現這一點。

我想我還會提到一個更容易(和可能更好看)的方式來做到這一點。看看「ScrollKit」,它工作得很好(儘管它使用一個UIScrollView):https://github.com/bobmoff/ScrollKit


如果您發現該UIScrollView中沒有通過觸摸高達touchesMoved,你可以嘗試一些不同的東西。
- 您可以添加一個UITapGestureRecognizer並在UIScrollView CanCancelContentTouchesYES上設置。
- 你也可以繼承的UIScrollView並重寫touchesMethods因此,如果用戶不滾動,觸覺信息將被傳遞了響應鏈:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent){ 

    if (!self.dragging){ 
     self.nextResponder.touchesBegan(touches , withEvent: event) 
    } 
    else{ 
     super.touchesBegan(touches , withEvent: event) 
    } 
} 

override func touchesMoved(touches: NSSet, withEvent event: UIEvent){ 

    if (!self.dragging){ 
     self.nextResponder.touchesMoved(touches , withEvent:event) 
    } 
    else{ 
     super.touchesMoved(touches , withEvent: event) 
    } 
} 

override func touchesEnded(touches: NSSet, withEvent: event UIEvent){ 

    if (!self.dragging){ 
     self.nextResponder.touchesEnded(touches , withEvent: event) 
    } 
    else{ 
     super.touchesEnded(touches , withEvent: event) 
    }  
} 
+0

我必須要能夠利用滾動內的節點上使用ScrollKit swift版本,touchesBegan中的代碼永遠不會運行。你有沒有UIPanGestureRecognizer的例子? – grape1

+0

@ grape1看看我的編輯,它應該幫助你觸摸方法和ScrollKit。使用UIPanGestureRecognizer進行自定義實現將會相當複雜,因爲如果它看起來不錯(因爲一切都將通過SKActions完成,並且您已經看到了這種情況)。就我個人而言,我從來沒有遇到過它的外觀問題,但我從來不需要創建一個長時間的滾動操作。爲此,我會爲了簡單起見而喜歡ScrollKit。 – MaxKargin

+0

ScrollKit對我所需要的太多了。一個簡單的垂直滾動節點,其中包含要選擇的字符網格。 – grape1