2015-05-24 55 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) 
相关问题