使用spritekit检测swipe swift释放时的坐标

时间:2016-01-22 05:54:24

标签: ios swift sprite-kit uigesturerecognizer swipe-gesture

如果有人可以帮助我,那就太棒了!

我需要获取滑动的坐标。我可以从locationInNode等获取初始触摸的坐标,但是如何才能获得滑动释放点上的坐标?

func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    touched = true

    for touch in touches {
        location = touch.locationInNode(self)
        print(location)
    }
}

这就是我获取初始坐标的方法,我也为touchesMoved

做了这个
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch in touches {
        location = touch.locationInNode(self)
        print(location)
    }

使用此代码时,它不会在触摸时打印释放的最终坐标。

基本上我想锻炼第一次触摸和用坐标释放滑动点之间的距离。 谢谢你的帮助!!

2 个答案:

答案 0 :(得分:2)

捕捉滑动的状态,从beganended

let point: CGPoint = recognizer.location(in: recognizer.view!)
if recognizer.state == .began {
    print(NSCoder.string(for: point))
}
else if recognizer.state == .ended {
    print(NSCoder.string(for: point))
}

答案 1 :(得分:2)

-(IBAction)swipe:(UIGestureRecognizer*)sender
{
    UIView *view1 = sender.view;

    if (sender.state == UIGestureRecognizerStateEnded) {
           CGPoint touchUpPoint = [sender locationInView:view1];
        NSLog(@"x=%f, y=%f",touchUpPoint.x,touchUpPoint.y);
    }

}