如何防止额外的触摸干扰拖动位置

时间:2015-05-24 18:39:01

标签: ios swift sprite-kit skspritenode

我正在寻找一个问题的帮助我已经停留了一段时间但却无法找到答案。

我有一个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)

    }
}

我的问题是,如果用户在拖动精灵时触摸屏幕的另一部分,则会出现毛刺并且Sprite在两个触摸位置之间来回反弹,并且无法确定要保留哪一个。< / p>

有没有人知道如何防止这种情况,以便Sprite始终遵循它最初被抓住的触摸?提前谢谢!

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

实现这一目标的最简单方法之一就是使用它(不需要实现touchesBegan):

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

expand扩展名仅用于增加允许触摸的边界,并定义为:

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

您可能希望添加更多逻辑来处理重叠等问题,但这应该是从哪里开始的良好基础。

希望这有帮助