Question

I am using the code below to fire a projectile (in a Sprite Kit based game) when the screen is "tapped", this all works fine. However I want to expand this so that handleTap is repeatedly called whilst the user "touches and holds down on the screen" Can anyone point me in the right direction?

// INITIAL SETUP
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setNumberOfTouchesRequired:1];
[view addGestureRecognizer:tapRecognizer];

.

// WHEN TAPPED
- (void)handleTap:(UITapGestureRecognizer *)recognizer {
    NSLog(@"%s", __PRETTY_FUNCTION__);
    [self setupAndFireProjectile];
}
Was it helpful?

Solution

Use UILongPressGestureRecognizer instead:

// INITIAL SETUP
UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleTouch:)];
[view addGestureRecognizer:recognizer];

.

// WHEN TAPPED
- (void)handleTouch:(UILongPressGestureRecognizer *)recognizer {
    NSLog(@"%s", __PRETTY_FUNCTION__);
    [self setupAndFireProjectile];
}

or use UIResponder's touchesBegan:withEvent: and touchesEnded:withEvent: methods:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
     // Start shooting
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
     // End shooting
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top