2015-05-24 57 views
0

我正在尋找一個問題,我一直堅持一段時間,但無法找到答案的幫助。如何防止額外的觸摸干擾拖動位置

我有一個SpriteKit兒童遊戲應用程序,用戶可以在其中圍繞屏幕拖動對象(SKspriteNodes)。這是在「觸摸」事件,控制,像這樣:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { 
    super.touchesBegan(touches, withEvent: event) 

    let touch = touches.first as! UITouch 
    let touchLocation = touch.locationInNode(self) 

    if piece01.containsPoint(touchLocation) && piece01Placed == false && aPieceIsBeingGrabbed == false { 
     piece01Grabbed = true 
     aPieceIsBeingGrabbed = true 
     self.piece01.zPosition = 4.0 
     } 
    } 

    override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) { 
    super.touchesMoved(touches, withEvent: event) 

    let touch = touches.first as! UITouch 
    let touchLocation = touch.locationInNode(self) 

    if piece01Grabbed && piece01Placed == false { 
     self.piece01.position = CGPointMake(touchLocation.x + worldScale * 120, touchLocation.y - worldScale * 80) 

    } 
} 

我的問題是,如果用戶觸摸屏幕的另一部分的同時拖動一個精靈,一個毛刺出現和雪碧來回反射兩者之間觸摸地點,並不能決定哪一個留下來。

有沒有人知道我可以如何防止這種情況,讓雪碧總是跟隨最初被抓住的那種感覺?提前致謝!

+0

我不能爲你提供目前一個完整的答案,但我想你指出正確的方向。首先遍歷集合中的所有觸摸,即觸摸觸摸{let thisTouch = touch as! UITouch;}'。 UITouch對象持續多點觸摸序列。你可以閱讀更多關於UITouch對象[這裏](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITouch_Class/#//apple_ref/occ/instp/UITouch/gestureRecognizers) – Mason

+0

非常感謝MaceDev ,我現在就研究一下。 – jwade502

回答

0

來實現,這將是使用這種最簡單的方法(沒有必要實施touchesBegan)一:凡expand擴展只是用來增加邊界

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) 
{ 
    for touch in (touches as! Set<UITouch>) 
    { 
     let location = touch.locationInNode(self) 

     for childNode in children 
     { 
      if (childNode is SKSpriteNode) 
      { 
       let node = childNode as! SKSpriteNode 

       if (CGRectContainsPoint(node.frame.expand(50.0), location)) 
       { 
        node.position = location 
       } 
      } 
     } 
    } 
} 

在觸摸是允許的,是定義爲:

extension CGRect 
{ 
    func expand(percent: CGFloat) -> CGRect 
    { 
     let deltaWidth = (percent/100.0) * self.width 
     let deltaHeight = (percent/100.0) * self.height 
     return CGRect(x: self.origin.x, y: self.origin.y, width: self.width + deltaWidth, height: self.height + deltaHeight) 
    } 
} 

您可能想要添加更多的邏輯來處理重疊等等,但這應該是從哪裏開始的良好基礎。

希望這有助於

+0

MaceDev,我開始研究你的方法,它效果很好。但是,我還發現了UIGestureRecognizer系列的觸摸檢測功能,並使用UIPanGestureRecognizer發現了一種更簡單的方法。 – jwade502

1

MaceDevs上述答案是正確的基礎上,我的問題,並與touchesMoved事件就像我最初是如何工作的問道。但是,我發現使用手勢識別器處理拖動事件更容易。所以爲了解決這個問題,我最終取消了我的「觸摸」方法,並實現了一個UIPanGestureRecognizer。

您所要做的就是在didMoveToView中設置一個UIPanGestureRecognizer,並將其最大觸摸次數設置爲1.如果不將該值設置爲1,則任何其他觸摸都會以平均觸摸方向移動棋子。

//Declare "draggingPiece" variable at top of swift file 
var draggingPiece: UIPanGestureRecognizer! 

//...init functions... 

override func didMoveToView(view: SKView) { 
    //Enable the gesture recognizer 
    draggingPiece = UIPanGestureRecognizer(target: self, action: Selector("dragPiece:")) 
    draggingPiece.maximumNumberOfTouches = 1 

    self.view?.addGestureRecognizer(draggingPiece) 

} 

func dragPiece(recognizer: UIPanGestureRecognizer) { 
    if recognizer.state == UIGestureRecognizerState.Began { 

    }else if recognizer.state == UIGestureRecognizerState.Changed { 

    }else if recognizer.state == UIGestureRecognizerState.Ended { 

    } 
} 

,然後刪除手勢識別在willMoveFromView:

self.view?.removeGestureRecognizer(draggingPiece) 
相關問題