Creating Sprite in touchesBegan and then moves with UIPanGestureRecognizer but small delay

StackOverflow https://stackoverflow.com/questions/23655601

  •  22-07-2023
  •  | 
  •  

Question

I'm creating an app with SpriteKit which creates a SKSpriteNode object in the touchesBegan and then when you Pan across the screen, it moves the object along with it.

However, when I touch the screen, the sprite is created as expected and then when I move my finger; the sprite remains in the same place for small delay then moves to where my finger is.

My code is like below:

-(void)handlePan: (UIPanGestureRecognizer*) gestureRecognizer{

if (([gestureRecognizer state] == UIGestureRecognizerStateBegan) || ([gestureRecognizer state] == UIGestureRecognizerStateChanged))
{

    CGPoint newLocation = [scene convertPointFromView:[gestureRecognizer locationInView:self.view]];
    scene.finger.position = newLocation;


}
if ([gestureRecognizer state] == UIGestureRecognizerStateEnded)
{
    NSLog(@"Ended");

    [scene.finger removeFromParent];
    scene.finger = nil;
}
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

for (UITouch *touch in touches) {
    CGPoint location = [self convertPointFromView:[touch locationInView:self.view]];
    [self addFingerSprite:location];
}

}

Any idea, how I can fix this small delay issue?

Was it helpful?

Solution

The delay is caused by the use of a PanGestureRecognizer. It takes a small time before the system recognizes that a pan is taking place. Use touchesMoved instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top