当屏幕被“点击”时,我使用下面的代码来发射射弹(在基于 Sprite Kit 的游戏中),这一切都工作正常。但是我想扩展它,以便在用户“触摸并按住屏幕”时重复调用handleTap,有人能给我指出正确的方向吗?
// 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];
}
最佳答案
使用UILongPressGestureRecognizer
相反:
// 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];
}
或使用UIResponder
's touchesBegan:withEvent:
和 touchesEnded:withEvent:
方法:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// Start shooting
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
// End shooting
}
关于ios - 如何扩展tap来处理连续触摸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21158154/